Handling the type of a variable is always an important thing to do in TypeScript if you don’t want your code to get an error and not work. So that, it’s very crutial to check the type of a variable in TypeScript. How to do it? Let’s go into detail now.
Check the type of a variable in TypeScript
Use typeof() method
Method typeof is an operator of Javascript syntax that help you to indicate the type of your variable by returning a string.
Syntax:
typeof(var)
Parameters:
- Var: variable that you want to check type.
Example:
const aString:string = 'Hello From LearnShareIT'
const aNumber:number = 123
const anObject:object = {
name:'Togban',
age:18,
country:'VietNam'
}
const anotherString:string = 'Welcome welcome'
console.log(`Type of aString: ${typeof(aString)}`)
console.log(`Type of aNumber: ${typeof(aNumber)}`)
console.log(`Type of anotherString: ${typeof(anotherString)}`)
console.log(`Type of anObject: ${typeof(anObject)}`)
console.log(`Type of '1': ${typeof('1')}`)
// We can also use number to convert a string to number type
console.log(typeof(Number('1'))) // output : number
console.log(typeof(function(){}))
console.log(typeof(typeof(aNumber)))//output: string (typeof always return a string)
console.log(`Type of boolean: ${typeof(true)}`)
console.log(`Type of: ${typeof(Math.ceil(1.5))}`)
Output:
Type of aString: string
Type of aNumber: number
Type of anotherString: string
Type of anObject: object
Type of '1': string
number
function
string
Type of boolean: boolean
Type of: number
You can also apply it to make a condition function.
Example:
const anString = "I like TypeScript"
const anNumber = 9
const handle =(something:string|number)=>{
if ( typeof(something)==='string'){
console.log(something)
}
if(typeof(something)==='number'){
console.log(something+1)
}
}
handle(anString)
handle(anNumber)
Output:
I like TypeScript
10
Remember always beware of what you put in the typeof method. Typeof method has some exceptions you can’t make sure of, which can turn your code into an error.
Example:
console.log(`Type of undefined:${typeof(undefined)}`)
console.log(`Type of null: ${typeof(null)}`)
console.log(typeof(!!10))
class Test{}
console.log(`Type of class: ${typeof(Test)}`)
const anArray:Array<number>=[0,1,2,3,4,5,6,7,8,9,10]
console.log(`Type of array: ${typeof(anArray)}`)//Result will be object because array is a special object
Output:
Type of undefined:undefined
Type of null: object
boolean
Type of class: function
Type of array: object
Summary
In this tutorial, we showed you how to check the type of a variable in TypeScript. We hope it’s helpful for you. If you have any questions or problems, please comment below. We will respond as possible. Thank you for your reading!
Maybe you are interested:
- Check for undefined in TypeScript
- Initialize Typed variable to Empty Object in TypeScript
- How To Declare Global Variables In TypeScript
Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm