How to Get a Variable Type in TypeScript
Last Updated :
18 Jul, 2024
Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific features like type guards.
Below are the approaches used to get a variable type in TypeScript:
Approach 1: Using typeof Operator
The typeof operator is a straightforward JavaScript feature that TypeScript also utilizes. It returns a string indicating the data type of its operand.
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
Approach 2: Using instanceof Operator
The instanceof operator is used to determine whether an object is an instance of a specific class or constructor function. This can be particularly useful for user-defined types.
Example: In this example, we are using instanceof Operator to get the type of a variable.
JavaScript
class MyClass {
// class definition
}
let myInstance = new MyClass();
if(myInstance instanceof MyClass){
console.log(`myInstance is an instance
of class MyClass and its
type is: ${typeof myInstance}`)
}
Output:
myInstance is an instance of class MyClass and its type is: object
Approach 3: Using Type Guards
Type guards in TypeScript allow you to narrow down the type of a variable within conditional blocks. These are particularly powerful because they enable TypeScript to recognize types in a way that impacts code flow analysis.
Example: In this example, we are using Type Guards to get the type of a variable.
JavaScript
function isString(value: any): void {
if (typeof value === "string") {
console.log("It's a string!");
}
else{
console.log(`Passed value is
of ${typeof age}
type.`)
}
}
let greet: any = "Hello, TypeScript!";
const age: any = 20;
isString(greet);
isString(age);
Output:
It's a string!
Passed value is of number type.
Similar Reads
How to Declare Variables in TypeScript ? In TypeScript, a variable can be defined by specifying the data type of the value it is going to store. It will store the values of the specified data type only and throws an error if you try to store the value of any other data type. You can use the colon syntax(:) to declare a typed variable in Ty
2 min read
TypeScript Type Annotations on Variables TypeScript is a statically typed superset of JavaScript that brings the benefits of strong typing to the JavaScript ecosystem. One of its key features is the ability to add type annotations to variables, which helps developers catch type-related errors at compile time rather than runtime. In this ar
3 min read
How To Get Types From Arrays in TypeScript? In TypeScript, arrays are a common data structure, but sometimes it's necessary to extract the types of elements stored in an array for type-checking or validation purposes. TypeScript provides several ways to extract types from arrays, enabling more type-safe operations.We will explore different me
3 min read
How to use Type Guards in TypeScript ? Here are the methods to use type guards in TypeScript:1. Using typeof Type GuardsThe typeof operator checks the type of a variable, primarily for primitive types like string, number, boolean, etc. JavaScriptfunction processValue(value: string | number) { if (typeof value === 'string') { console.log(
3 min read
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read