← TypeScript learning pathTypeScript · Module 3 of 10
Functions
Type parameters, return values, optional and default arguments.
📖 Key ideas
- function add(a: number, b: number): number { return a + b; }
- Optional parameter: name?: string.
- Default parameter: greeting = "Hi".
- Arrow function types: (x: number) => string.
- 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[]