What is type inference in TypeScript?

Beginner

Answer

Type inference is TypeScript's ability to automatically determine types without explicit type annotations. The compiler analyzes the code and infers the most appropriate types.

let message = "Hello"; // TypeScript infers string type
let count = 42; // TypeScript infers number type
let isActive = true; // TypeScript infers boolean type

function add(a: number, b: number) {
  return a + b; // Return type inferred as number
}