Generic code

Generic functions and constraints, generic classes and typed collections, and the built-in utility types.

About 5 hours of study

Learning objectives

Lessons

  1. 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. 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. 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

Module quiz

1. What is T in first<T>(items: T[]) called with [1, 2]?
2. What does Map<string, number>.get return?
3. Which utility type makes every property optional?
4. Why add the constraint T extends { length: number }?
5. What is Omit<User, 'password'>?
6. Why does Record<Priority, number> catch a missing priority when Priority is a literal union?

‹ Classes and enumsAdvanced types and real programs ›