← TypeScript learning path
TypeScript · Module 3 of 10

Functions

Type parameters, return values, optional and default arguments.

📖 Key ideas

  1. function add(a: number, b: number): number { return a + b; }
  2. Optional parameter: name?: string.
  3. Default parameter: greeting = "Hi".
  4. Arrow function types: (x: number) => string.
  5. Rest parameters: ...nums: number[].
💡 Real-life example: A typed function is a vending machine slot: it only accepts the right coin and gives the promised snack.
Return typeType after the parameter list
Optional parameterParameter with ?
Function type(a: T) => R
Rest parameter...args: T[]