Type inference

TypeScript tries to find the types of the variables and objects in our application using what is known as type inference. When we declare a variable, TypeScript will try to observe the value assigned to the variables in the application to identify its type. Let's examine some examples:

let myVariable1 = 3;

The type of the variable myVariable1 is inferred as a number.

let myVariable2 = "Hello";

The type of the variable myVariable2 is inferred as a string.

let myVariable3 = {  name: "Remo",  surname: "Jansen",  age: 29};

The type of the variable myVariable3 is inferred as the following type:

{ name: string; surname: string; age: number; }

The type any is assigned in the cases in which TypeScript is not able to identify the type ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.