Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
Mapped Types in TypeScript allow creating new types based on existing ones by iterating over keys and modifying them.
It's like map() for types — you can iterate over each object key and set the needed value for it.
type NewType = {
[Key in Union]: Type;
}
Key — variable name representing current key from Union set.Type — value type for each key.keyof, modifiers (?, readonly), and utility types (Pick, Partial, etc.).type User = {
name: string;
age: number;
};
// Make type where all values are boolean
type UserPermissions = {
[K in keyof User]: boolean;
};
// => { name: boolean; age: boolean }
Partial<T>)type Optional<T> = {
[K in keyof T]?: T[K];
};
Usage
type User = { name: string; age: number };
type OptionalUser = Optional<User>;
// => { name?: string; age?: number }
Readonly<T>)type ReadOnly<T> = {
readonly [K in keyof T]: T[K];
};
type Mutable<T> = {
-readonly [K in keyof T]: T[K];
};
Partial, Pick, Readonly, Record, etc.)GraphQL, API, Form systems)