Defining Primitive DataTypes

You can indicate a var as a number, boolean or String using the below command.

var a : number;
var b : boolean;
var c : String;
var d : number|boolean //number or boolean can be stored in this variable
a = 10;
b = true;
c= "Hello World";
d=true;

If you assign a incorrect type to a variable then TypeScript compilation will fail
Example : Below

var a : number;
var b : boolean;
var c : String;

a = 10;
b = true;
c= 20;

Compilation of above code will give you error :

Leave a Comment