Type Annotations

Type annotation can be done by specifying type after a : (colon).

Examples:

TS
let num: number = 20;

function addNumbers(a: number, b:number ): number {
	return a + b;
}

const addNumbers: number = (a: Number, b: Number) => {
	return a + b;
}

Type-Inference

In typescript, you don't always have to define a type. It is smart enough to infer the correct type from the provided information.

Example:

TS
const num = 20; // num is inferred as number as the value provided is number type.

const store = ["Adesh", 20]; // the type here is inferred as ('string'|'number')[]
Adesh Tamrakar
SOFTWARE ENGINEER · VAULT

Notes, insights and random discoveries from a working engineer's vault - written for future me, published for you.