December 2017
Beginner
372 pages
10h 32m
English
As we discussed in the Types in TypeScript section, TypeScript is an optional statically typed language, which means that TypeScript does provide types, but explicitly assigning types to variables is optional. TypeScript has an ability to infer types if not declared. Let's look at an example:
let firstName = "John";firstName = 10;
In the line firstName = 10, the compiler alerts us with the error that the variable firstName is a string, and a number cannot be assigned to a string. How does TypeScript identity this mismatch? TypeScript uses type inference to identify the type for the firstName variable.
In this case, TypeScript identified based on the assignment of the value at the time of variable declaration. If we had not ...
Read now
Unlock full access