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:
this will print the output to console only if cityElevation variable is of type number.
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