In this post, we are going to see how TypeScript can infer type in many ways. Type inference happens when there is no explicit type annotation.
Basic Variable
The variable value can infer type.
let name = 'john doe'; // type of name is `string`
let age = 17; // type of age is `number`
let isStudent = true; // type of isStudent is `boolean`
name = 25; // compile error
age = 'wife of john doe'; // compile error
isStudent = 'yes'; // compile error
Array
Type inference for array will be based on its elements and it can be mixed types.
let students = ['donna', 'robin', 'bruce']; // type of students is `string[]`
let ages = [10, 15, 20]; // type of ages is `number[]`
let mixed = ['donna', 10, 15, 20, 'robin', 'bruce']; // type of mixed is `(string|number)[]
Object
Just like variable, type inference for object works similar way.
let student = {
name: 'donna',
age: 18
}
student.name = 20; // compile error
Function
Type inference can happen for function return when there is enough type annotation given. See example below:
function substract(num1: number, num2: number) {
return num1 - num2;
}
Return of substract
method will be inferred as number
because num1
and num2
type is number
.