Mapped Types and Conditional Types
Build mapped types to transform all properties of a type and use conditional types with infer for advanced type-level logic.
Mapped Types — Transform Every Property
Mapped types iterate over the keys of a type and transform them. They're the foundation of Partial, Readonly, and Record utility types.
// Partial implemented as a mapped type:
type MyPartial<T> = {
[K in keyof T]?: T[K];
};
// Readonly:
type MyReadonly<T> = {
readonly [K in keyof T]: T[K];
};Remapping Keys with as
Use as in the mapping clause to remap key names. Combine with template literal types for powerful transformations.
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type UserGetters = Getters<{ name: string; age: number }>;
// { getName: () => string; getAge: () => number }All lessons in this course
- Generics: T extends and constraints
- Utility Types: Partial Required Pick Omit
- Mapped Types and Conditional Types
- Narrowing: typeof instanceof discriminated unions