How to usetypeof Operator in Typescript
You can use the typeof
operator to get the type of a variable at runtime. It returns a string representing the type of the operated variable.
Example: In this example, we are using typeof Operator to get the type of a variable.
Javascript
let myVar: number = 42; console.log( typeof myVar); |
Output:
number
How to Get a Variable Type in TypeScript ?
To get a variable type in TypeScript, we have different approaches. In this article, we are going to learn how to get a variable type in Typescript.
Below are the approaches used to get a variable type in TypeScript:
Table of Content
- Using typeof Operator
- Using instanceof Operator
- Using Type Guards