Docs
/
TypeScript
Chapter 4

04 — Type Inference & Annotations

Core Concepts

  • Type Inference — TypeScript automatically determines types from values
  • Explicit Annotations — manually specifying types with : Type syntax
  • Contextual Typing — type inferred from where the expression appears (callbacks, etc.)
  • const Assertionsas const narrows to the most specific (literal) type
  • satisfies Operator — validates type compatibility without widening (TS 5.0+)
  • Return Type Inference — functions infer return types from return statements
  • Best Common Type — TS picks the best type that fits all array elements

When to Annotate vs Infer

ScenarioAnnotate?Why
Variable with initializerNoInferred from value
Function parametersYesCan't be inferred
Function return typeSometimesAnnotate public APIs
Object literalsSometimesUse satisfies for validation
ConstantsNoUse as const for literals
Complex genericsSometimesHelp TS when inference fails