Generic code
Generic functions and constraints, generic classes and typed collections, and the built-in utility types.
About 5 hours of study
Learning objectives
- Write generic functions with constraints
- Build generic data structures
- Derive types with utility types
Lessons
- 1
Generic functions Advanced
Type parameters that let one function work with many types without losing type information: inference of type arguments, several type parameters, constraints with extends, and keyof constraints.
- 2
Generic classes and typed collections Advanced
Generic classes and interfaces, default type parameters, the typed collections Map, Set and ReadonlyArray, and building your own data structures that work for any item type.
- 3
Utility types Advanced
The built-in types that transform other types: Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, NonNullable, ReturnType and Parameters, and when to use each.
Module practice
- TypeScript: Generic list helpersEasy
- TypeScript: A generic groupByMedium
- TypeScript: A generic binary searchHard
- TypeScript: Check brackets with a generic stackEasy
- TypeScript: A generic LRU cacheMedium
- TypeScript: A generic priority queueHard
- TypeScript: Fill in options with defaultsEasy
- TypeScript: A grade histogram with RecordMedium
- TypeScript: Compare two versions of a catalogueHard