03 / 08

What is 'type inference' in TypeScript?

Type inference in TypeScript is a feature where the compiler automatically determines the type of a variable, function, or expression without the need for explicit type annotations from the developer. This enhances productivity by reducing the amount of boilerplate code, while still maintaining the benefits of static typing

  1. 1

    Default Parameters: When a default value is assigned to a parameter, TypeScript infers its type from that value.

  2. 2

    Contextual Typing: TypeScript infers the types of variables based on their usage, such as in callbacks or event listener

  3. 3

    Function Return Type: If a function returns a value, TypeScript infers the return type based on the function body.

  4. 4

    Variable Inference: When you declare a variable and initialize it with a value, TypeScript infers the type from the assigned value.