TypeScript theorytheory 0/50 · 0%
Functions · easy

7. Functions: params, return types, optional & default

Typing inputs, outputs, and flexible parameter lists.

Function parameters and return types can both be annotated. Optional parameters use `?` and must come after required ones; default parameters supply a fallback and their type is inferred from the default.

ts
function mint(to: string, amount: number, memo?: string): boolean {
  return amount > 0;
}

function fee(gwei: number, multiplier: number = 1.1): number {
  return gwei * multiplier;
}

Arrow functions type the same way: `const add = (a: number, b: number): number => a + b;`. If you omit the return type, TypeScript infers it from the function body — a good habit for internal helpers, while public APIs benefit from explicit return types for documentation and stability.

Check your understanding

  1. 1. Where must optional parameters appear in a parameter list?

  2. 2. What determines the type of a default parameter if not annotated?

  3. 3. Why annotate return types on public functions?