In this typescript tutorial, we will see how to check type of variable in typescript. Here is the topic we are going to cover:
- Typescript check type of variable
- Typescript check type of variable array
- Typescript check type of variable interface
- Typescript check type of variable custom type
- Typescript check type of variable string
- Typescript check if variable is type of class
Typescript check type of variable
Here we will see how to check the type of variable in typescript.
To check the type of variable we can use typeof operator in typescript. For example ‘if(typeof var== ‘country’){}’, here in TypeScript, the typeof operator produces a string indicating the type of the value and may be used as a type guard.
Let’s see an example of how we can check the type of variable.
In the code editor, create a new file, CheckVariableType.ts, and write the below code
const EmpName: any = 'Adam';
const age: any = 25;
const isActive: any = true;
var x = typeof EmpName;
var y = typeof age;
var z = typeof isActive;
console.log('EmpName is of ' +x+ ' type')
console.log('age is of ' +y+ ' type')
console.log('isActive is of ' +z+ ' type')
To compile the code and run the below command and you can see the result in the console.
ts-node CheckVariableType

This is an example of Typescript check type of variable.
Read How to add a property to object in Typescript
Typescript check type of variable array
Here we will how to check the type of variable is an array or not in typescript.
For example, we will check the type of variable is an array or not, and also we will check the type of value in the array.
In the CheckVariableType.ts, write the below code:
const arr: string[] = ['a', 'b', 'c'];
//check the type of variable
const isArray = Array.isArray(arr); // 👉️ true
console.log(isArray)
//check the type of values in array
if (Array.isArray(arr)) {
const isStringArray =
arr.length > 0 &&
arr.every((value) => {
return typeof value === 'string';
});
console.log(isStringArray); // 👉️ true
}
To compile the code and run the below command and you can see the result in the console.
ts-node CheckVariableType

This is an example of Typescript check type of variable array.
Typescript check type of variable interface
Here we will see an example of check type of variable interface in typescript.
For example, we will define two types (Apple and grape) and then we will create a function that accepts an object and then checks the type is Apple, if it is true, then it will return the properties. Then we will define an object, which can be Apple or Grape type, and also we will provide the value to the properties.
At last, we will check the type of object, whether it is Apple type or Grape type.
In the CheckVariableType.ts file, write the below code:
interface Apple {
appleColor: string;
}
interface Grape {
grapeColor: string;
}
function isApple(object: any): object is Apple {
return 'appleColor' in object;
}
let obj: Apple | Grape = {
grapeColor: "Green"
}
if (isApple(obj)) {
console.log("`object` has type `Apple`")
} else {
console.log("`object` has type `Grape`")
}
To compile the code and run the below command and you can see the result in the console.
ts-node CheckVariableType

This is an example of Typescript check type of variable interface.
Read Data types in typescript with examples
Typescript check type of variable custom type
Here we will see an example to check the type of variable is of custom type in typescript.
For example, we will assign an array to a variable (const fruit = [“apple”, “banana”, “grape”] as const). Then we will define type alias ‘ Fruit’, which is the type of ‘[“apple”, “banana”, “grape”]’. Next, we will define an element x is the type of Fruit, and then we will use the include method to check whether the fruit is included or not.
Then we define two variables: MyFruit, which is fruit type, having value ‘apple’, and myOrange, which contains ‘Orange’.
Next in if else we will pass the variable and check whether the type is fruit type or not.
In the CheckVariableType.ts file, write the below code:
const fruit = ["apple", "mango", "grape"] as const;
type Fruits = (typeof fruit)[number];
const isFruit = (x: any): x is Fruits => fruit.includes(x);
let myfruit : Fruits = "apple";
let myOrange = "Orange";
if (isFruit(myfruit)) {
console.log("My fruit is of type 'Fruit'");
}
else{
console.log('It is of different type')
}
To compile the code and run the below command and you can see the result in the console:
ts-node CheckVariableType

This is an example of Typescript check type of variable custom type.
Typescript check type of variable string
Here we will see an example of a type check of a variable string in typescript.
For example, we will define a variable as ‘Country’ and assign a value as “Africa”. Then we will check the type of variable using typeof().
In the CheckVariableType.ts file, write the below code:
var country="Africa"
var result= typeof(country);
console.log(result)
To compile the code and run the below command and you can see the result in the console.
ts-node CheckVariableType

This is an example of a Typescript check type of variable string.
Typescript check if the variable is a type of class
Here we will see an example of to check if the variable is a type of class in typescript.
For example, we will define a class Fruit, then we will define the constructor inside the class, then check the variable is a type of class by using the instanceof operator, which will return true or false.
In the CheckVariableType.ts file, write the below code:
class Fruit {
name:any;
constructor(name:any) {
this.name = name;
}
}
const fruit = new Fruit('fluffy');
// true because Fruit in on the prototype chain of fruit
console.log(fruit instanceof Fruit); // true
// Proof that Fruit is on the prototype chain
console.log(Object.getPrototypeOf(fruit) === Fruit.prototype); // true
// true because Object in on the prototype chain of fruit
console.log(fruit instanceof Object);
// Proof that Object is on the prototype chain
console.log(Object.getPrototypeOf(Fruit.prototype) === Object.prototype); // true
console.log(fruit instanceof Function); // false, Function not on prototype chain
console.log(typeof Fruit)
To compile the code and run the below command and you can see the console.
ts-node CheckVariableType

This is an example of a Typescript checking if the variable is a type of class.
Conclusion
In this Typescript tutorial, we saw how we can check the type of the variable by taking different examples in typescript. Here are the topics we covered:
- Typescript check type of variable
- Typescript check type of variable array
- Typescript check type of variable interface
- Typescript check type of variable custom type
- Typescript check type of variable string
- Typescript check if the variable is a type of class
You may like the following typescript tutorials:
- Typescript array concatenation
- How to reverse an array in Typescript
- Typescript sort array
- Typescript array find
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com