In JavaScript, there are six primitive data types: boolean, number, string, symbol (new as of ES6), as well as null and undefined. TypeScript includes all of these as built-in types.
Boolean
Booleans have two logical values: true or false.
letisReady:boolean=false;isReady='yes';// Error: a string is not assignable to type 'boolean'isReady=true;// OK
Number
All numbers in TypeScript are stored no differently than in JavaScript — as IEEE 754 double-precision floating-point numbers.
constinteger:number=6;constfloat:number=6.66;// Non-decimal numbers are cool too:consthex:number=0xf00d;constbinary:number=0b1010;constoctal:number=0o744;// Let's not forget our weird friends:constnegativeZero:number=-0;constactuallyNumber:number=NaN;constlargeNumber:number=Number.MAX_VALUE;constpi:number=Math.PI;constlargestNumber:number=Infinity;
String
Strings work the same way as in JavaScript, where both ' and " work correctly, as well as ` for template literals.
Symbol
Symbol is a unique and immutable data type introduced in ES6.
Null and Undefined
Both undefined and null have their own types named undefined and null, respectively.
By default null and undefined are subtypes of all other types, which means you can assign null and undefined to something having another type. For example:
const name: string = 'Christine';
const nameTagLabel: string = `Hello, my name is ${name}.`;
let country: symbol = Symbol('Canada'); // OK
country: symbol = 'Canada'; // Error: a string is not assignable to type 'symbol'
// typeof country === "symbol"
// Not much else we can assign to these variables!
let u: undefined = undefined;
let n: null = null;
// To illustrate:
u = 200; // Error: a number is not assignable to type 'undefined'
n = 'idk'; // Error: a string is not assignable to type 'null'
let myNum: number = 500;
myNum = null; // OK
myNum = undefined; // also OK