How to use typeof operator in TypeScript?


In TypeScript, the typeof is one of the most helpful operators and keywords to check the types of variables or identifiers. However, TypeScript is a type-strict language. So, we need to define the type of the variable or object while defining the identifier itself. Still, sometimes we need to check the type of the variable.

For example, we are taking the data from the user via forms and handling it via TypeScript. Then we have to validate the data and perform some database operations. Also, the identifier can have multiple types in TypeScript. So, using the typeof operator, we can check the identifier type after initializing it.

Syntax

The below syntax demonstrates the use of typeof operator with the identifiers.

let type = typeof <variable>

Parameters

  • Variable − It is an identifier, for which we need to check the type.

Return Value

It returns a string according to the data type of the operand of the typeof operator.

Example 1

In this example, we have used the typeof operator with the string, number, boolean, and undefined variables. Also, we used the typeof operator with the variable containing the null as a value.

In the output, we can see that It returns the string representing the type of the identifier. For the null_value1 identifier, the typeof operator returns the object.

// Defining the variables of particular type and checking its type.
let str1: string = "TutorialsPoint";
console.log(typeof str1);
let num1: number = 32;
console.log(typeof num1);
let bool1: boolean = true;
console.log(typeof bool1);
let un_defined;
console.log(typeof un_defined);
let null_value1 = null;
console.log(typeof null_value1);

On compiling, it will generate the following JavaScript code −

// Defining the variables of particular type and checking its type.
var str1 = "TutorialsPoint";
console.log(typeof str1);
var num1 = 32;
console.log(typeof num1);
var bool1 = true;
console.log(typeof bool1);
var un_defined;
console.log(typeof un_defined);
var null_value1 = null;
console.log(typeof null_value1);

Output

The above code will produce the following output −

string
number
boolean
undefined
object

Example 2

In the example below, we have created the objection containing some key-value pairs, a function returning the number value, and an array of numbers that contains different values.

Also, we used the typeof operator to check the type of all. The typeof operator returns the “object” string for the object, the “function” string for the Function, and the “object” string for the array, as the array is the instance of the object.

// using the typeof operator with object, function, and array.
let demo_obj = {
   prop: "value",
};
console.log(typeof demo_obj);
function func() {
   let a = 10 + 20;
   return a;
}
console.log(typeof func);
let array: Array<number> = [4, 23, 212, 2123, 323, 3];
console.log(typeof array);

On compiling, it will generate the following JavaScript code −

// using the typeof operator with object, function, and array.
var demo_obj = {
   prop: "value"
};
console.log(typeof demo_obj);
function func() {
   var a = 10 + 20;
   return a;
}
console.log(typeof func);
var array = [4, 23, 212, 2123, 323, 3];
console.log(typeof array);

Output

The above code will produce the following output −

object
function
object

Example 3

In this example, we checked the type of the NaN keyword using the typeof operator, which returns the “number” string. The type of negative Infinity is also a number. The PI is the property of the Math class, which contains the number value. That’s why the typeof operator returns “number” for the Math.PI property.

The pow() is the Math library function to get the power of any numbers. So, the typeof operator returns the “function” for the Math.pow() method.

// using the typeof operator with NaN, infinity, library methods, and members.
let NaN_type = NaN;
console.log(typeof NaN_type);
let infi = -Infinity;
console.log(typeof infi);
let pI = Math.PI;
console.log(typeof pI);
let pow = Math.pow;
console.log(typeof pow);

On compiling, it will generate the following JavaScript code −

// using the typeof operator with NaN, infinity, library methods, and members.
var NaN_type = NaN;
console.log(typeof NaN_type);
var infi = -Infinity;
console.log(typeof infi);
var pI = Math.PI;
console.log(typeof pI);
var pow = Math.pow;
console.log(typeof pow);

Output

The above code will produce the following output −

number
number
number
function

Example 4

In this example, we used the typeof operator to get the type of variable1 and defined variable2 of the same type as variable1. Also, we used the type alias to store the type of variable3. The type named type1 contains the type of variable3, which is a string.

After that, we used type1 to define the type of variable4. In the output, users can observe that the type of variable4 is the same as variable3.

// defining the variable1 of type number
var variable1: number = 10;

// To define the variable2 as of same type variable1
let variable2: typeof variable1 = 20;
console.log(typeof variable2);

// variable3 of string type
var variable3: string = "Hello!";

// using the type alias to define the type of same type as variable3
type type1 = typeof variable3;

// defining the variable4 of type named type1
let variable4: type1 = "Hi";
console.log(typeof variable4);

On compiling, it will generate the following JavaScript code −

// defining the variable1 of type number
var variable1 = 10;

// To define the variable2 as of same type variable1
var variable2 = 20;
console.log(typeof variable2);

// variable3 of string type
var variable3 = "Hello!";

// defining the variable4 of type named type1
var variable4 = "Hi";
console.log(typeof variable4);

Output

The above code will produce the following output −

number
string

We learned to use the typeof operator in TypeScript in this tutorial. Using various examples, we have seen how the typeof operator works with different data types. If we use the constructor to create number, boolean, or string variables, then the typeof operator returns the “object” as its type.

Updated on: 03-Jan-2023

687 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements