Typeof command in TypeScript
User
kindly explain use of typeof command in typescript
ChatGPT
In TypeScript, the typeof operator is used to determine the type of a value or variable at runtime. It allows you to check the type of a variable or an expression and perform conditional logic based on that type. The typeof operator returns a string representing the data type of the given operand.
The general syntax of the typeof operator in TypeScript is:
typescript
Copy code
typeof operand
Here, operand can be a variable, a literal value, or an expression. The result of the typeof operator will be a string representing the type of the operand.
Let's look at some examples to understand its usage better:
Example 1: Checking primitive types
typescript
Copy code
let x: number = 10;
let y: string = "Hello";
let z: boolean = true;
console.log(typeof x); // Output: "number"
console.log(typeof y); // Output: "string"
console.log(typeof z); // Output: "boolean"