-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve modifiers in homomorphic mapped types #12563
Conversation
@DanielRosenwasser and @sandersn, we will need to clarify in the handbook that there is a subset of mapped types (isomorphic mapped types) that we treat them specially in inference, and in trafficking the optional and readonly modifiers through the transformation. |
@ahejlsberg How can I create a Edit: Nevermind, read what you said about |
@DanielRosenwasser brings a good point about terminology. these are actually "homomorphic" mapped types and not "isomorphic" since the transformation is not bidirectional (i.e |
@DanielRosenwasser Changing to "homomorphic" makes everything so much clearer! 😛 |
Is there any way currently to get rid of that undefined? I'm trying to write a |
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 withT
(because it has the same set of properties asT
) and now preserves the optional and readonly modifiers as they exist on the properties inT
.Since the predefined mapped types
Partial<T>
andReadonly<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.Fixes #12542.