Docs
/
TypeScript
Chapter 6

06 — Functions & Overloads

Core Concepts

  • Parameter types — must always be annotated
  • Return types — inferred, but annotate public APIs
  • Optional parametersparam?: Type (always at the end)
  • Default parametersparam: Type = defaultValue
  • Rest parameters...args: Type[]
  • Function overloads — multiple signatures, one implementation
  • this parameter — explicit this typing in function signatures
  • Callable typestype Fn = (x: number) => string
  • void vs undefined — void callbacks allow returning values; undefined doesn't

Overloads vs Union Returns

ApproachWhen to Use
OverloadsReturn type depends on input type
Union returnReturn could be either type regardless of input
GenericSame structure, different types