TypeScript Notes - 1.0

TypeScript Notes - 1.0


Primitive Types

In TypeScript, you can find different data types,

let isPresent: boolean = false;

let magic: number = 12;

let hello: string = "Wow";


let notDefined: undefined;

let notPresent: null = null;

let someSymbol: symbol = Symbol("star")

let biggy: bigint = 24n;

Structural Typing

type Point2D = { x: number; y: number };

type Point3D = Point2D & { z: number };


let point2D: Point2D = { x: 0, y: 0 };

let point3D: Point3D = { x: 0, y: 0, z: 0 };

DuckTyping

point2D = point3D;

In this comparison, point2d has all the elements that are in the point3D object, we call this duck typing.

point3D = point2D; /// Compile error: Property 'z' is missing in type 'Point2D' but required in type

^This results in a compile error because point2D does not have the property `Z` in it.

Object Types and Type Aliases

//Type alias

type Point = { x: number; y: number };

//Object type using the declared type alias

let center: Point = {  x: 1,  y: 2,};

Array Tuples