TypeScript
Mapped Types
Construct new types from existing ones.
By EZ4Code Team
mapped-typeadvanced-type
Code
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
type Optional<T> = {
[P in keyof T]?: T[P];
};
type Mutable<T> = {
-readonly [P in keyof T]: T[P];
};
type Nullable<T> = {
[P in keyof T]: T[P] | null;
};
interface User { id: number; name: string; }
type ReadonlyUser = Readonly<User>;
type OptionalUser = Optional<User>;Explanation
Mapped types batch-modify type properties by iterating over keys.