TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. Specifically, a readonly
or ?
property modifier in a mapped type can now be prefixed with either +
or -
to indicate that the modifier should be added or removed.
type MutableRequired<T> = { -readonly [P in keyof T]-?: T[P] }; // Remove readonly and ?
type ReadonlyPartial<T> = { +readonly [P in keyof T]+?: T[P] }; // Add readonly and ?
Example:
type MutableRequired<T> = T extends object ? {-readonly [K in keyof T]: T[K]} : T; interface Book { readonly name: String; } const newState: MutableRequired<Book> = { name: 'st' };
newState's anme is mutable and required.