Warning: session_start(): open(/tmp/sess_315deb04168e4f68e175fd59969ad12a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check The Type Of A Variable In TypeScript? - LearnShareIT

How To Check The Type Of A Variable In TypeScript?

Check the Type of a Variable in TypeScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *