TypeScript Inference

This lesson describes how TypeScript infers types when we omit to explicitly provide one.

Control flow analysis

Since version 2.0, TypeScript checks the type of all possible flows. This means that TypeScript analyzes all paths to discover the most specific type a variable can be. In other words, if you define a variable to be many types, with a union |, TypeScript knows, line by line, what the actual type of the variable is. This is useful to get code to completion.

Code completion is when the developer types and the editor is suggesting functions or properties available. Good code completion editors provide useful possible code depending on the type.

Get hands-on with 1200+ tech skills courses.