Backbencher.dev

Type Inference in TypeScript

Last updated on 6 Jan, 2023

In TypeScript, we can explicitly define the type of a variable as shown below:

let age: number = 5;

But in the above case, even without the explicity number type, TypeScript can infer the type by the value assigned, which is 5.

Observe the code below.

let age = 5;
age = "Five";

The second line throws an error because TypeScript already infered the type of age as number.

Type inference
--- ○ ---
Joby Joseph
Web Architect