You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
@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 Writable<T>, now? I had been using the stripping of modifiers to allow for temporarily writable versions of immutable types during their piecemeal construction.
Edit: Nevermind, read what you said about Pick. Seems like a bit of a hack at a glance; would it be useful to add a Writable<T> (and Required<T>) alias(es) to the lib?
@DanielRosenwasser brings a good point about terminology. these are actually "homomorphic" mapped types and not "isomorphic" since the transformation is not bidirectional (i.e T => ReadOnly<T>, but not the opposite)
In --strictNullChecks mode, the type T6 above is actually { a: number | undefined, b: number | undefined } because the undefined that was added as a result of making the properties optional persists even when the modifiers are stripped.
Is there any way currently to get rid of that undefined? I'm trying to write a get function, similar to Lodash's get (https://lodash.com/docs/4.17.2#get) and it's almost possible except for the fact that (T | undefined)[keyof T] is never and I can't make the get function work more than one level deep because of that.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
5 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.