Function Types and Signatures
- Function type expressions. A function type writes the call shape: (order: Order) => number names parameters and result.
- Typing callbacks. Typing a callback parameter documents the contract: onSave: (draft: Draft) => Promise<void>.
- void and never. void means the return is ignored; never means the function cannot return at all: it throws or loops.
Optional, Default and Rest Parameters
- Optional parameters. A trailing question mark makes a parameter optional: and its type quietly gains undefined inside the body.
- Defaults. A default value types the parameter and removes undefined at once: function page(size = 20).
- Rest parameters. A rest parameter collects the tail into a typed array: function log(...parts: string[]).
Overloads and Callback Types
- Overloads. Overloads declare several call shapes above one implementation: parse(x: string): Date and parse(x: number): Date.
- Prefer unions first. Most overloads are better written as a union parameter: reach for overloads only when returns differ by input.
- this typing. A function can declare its expected this type, catching detached-method bugs at compile time.
Before and after this level
- Before: Level 3: Unions & Narrowing: Union and Intersection Types, Type Guards and Control-Flow Analysis, Discriminated Unions.
- Next: Level 5: Generics: Generic Functions and Types, Constraints and Relationships, Designing Reusable Typed APIs.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does TypeScript Level 4: Functions cover?
Function Types and Signatures, Optional, Default and Rest Parameters, Overloads and Callback Types: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 3: Unions & Narrowing, which covers Union and Intersection Types, Type Guards and Control-Flow Analysis, Discriminated Unions.