Saturday, November 3, 2018

TypeScript - DataTypes - check types at runtime

To check types in JavaScript we have the typeof function. Since, TypeScript is a superset of JavaScript - we can use this JavaScript function to evaluate the type at runtime:

let cityElevation:string | number = 505;

if(typeof(cityElevation) == "number"){
console.log("cityElevation is a "+typeof(cityElevation));
}

this will print the output to console only if cityElevation variable is of type number.

No comments:

Post a Comment