Author : HASSAN MD TAREQ

Data Types

  • Boolean: boolean
  • Number: number
  • String: string
  • Array: number[]
  • Tuple: [string, number] = [ , ]
  • Enum: enum Foo { , , }
  • Any: any
  • Void: void
  • Null: null
  • Undefined: undefined
  • Never: never
  • Object: object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, bigint, symbol, null, or undefined
  • Optional type: x?: number

Details:

Variables

let identifier: type = value;

// type inference
let identifier = value;

Example

let isDone : boolean = false;

let roll: number = 6;

let fullName: string = "TAREQ HASSAN";

let fooList: number[] = [1, 2, 3];
let fooList: Array<number> = [1, 2, 3];

let coordinate: [string, number] = [14, 75];

enum Color { Red = 1, Green, Blue };
let c: Color = Color.Green;

let notSure: any = 4;

function warnUser(): void { }

let nuru: null = null;

let unDefu: undefined = undefined;

function error(message: string): never { throw new Error(message); }

See: https://www.typescriptlang.org/docs/handbook/variable-declarations.html#variable-declarations

Constant

const PI = 3.1416;

See: https://www.typescriptlang.org/docs/handbook/variable-declarations.html#const-declarations