Preserve modifiers in homomorphic mapped types by ahejlsberg · Pull Request #12563 · microsoft/TypeScript
WIth this PR we preserve property modifiers in homomorphic (structure preserving) mapped types. A mapped type of the form { [P in keyof T]: X } is homomorphic with T (because it has the same set of properties as T) and now preserves the optional and readonly modifiers as they exist on the properties in T.
Since the predefined mapped types Partial<T> and Readonly<T> are homomorphic they now preserve already existing property modifiers.
EDIT: With #12826 the predefined Pick<T, K> type now also preserves property modifiers. Before that PR, Pick<T, K> could be used to strip modifiers, but that is no longer possible.
type T1 = { a?: number, b: string }; type T2 = Partial<T1>; // { a?: number, b?: string } type T3 = Readonly<T1>; // { readonly a?: number, readonly b: string } type T4 = Partial<Readonly<T1>>; // { readonly a?: number, readonly b?: string } type T5 = Readonly<Partial<T1>>; // { readonly a?: number, readonly b?: string } type T6 = Pick<T1, "a">; // { a?: number }
Fixes #12542.