Skip to content
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

Merged
merged 3 commits into from Nov 30, 2016
Merged

Conversation

@ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Nov 29, 2016

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.

@DanielRosenwasser DanielRosenwasser changed the title Preserve modifiers in isomorphic mapped types Preserve modifiers in homomorphic mapped types Nov 29, 2016
@mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Nov 29, 2016

@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.

@weswigham
Copy link
Member

@weswigham weswigham commented Nov 29, 2016

@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?

@mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Nov 29, 2016

@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)

@ahejlsberg
Copy link
Member Author

@ahejlsberg ahejlsberg commented Nov 29, 2016

@DanielRosenwasser Changing to "homomorphic" makes everything so much clearer! 😛

@ahejlsberg ahejlsberg merged commit aaf0c89 into master Nov 30, 2016
3 checks passed
@ahejlsberg ahejlsberg deleted the mappedTypeModifiers branch Nov 30, 2016
@ahejlsberg ahejlsberg mentioned this pull request Nov 30, 2016
@mhegazy mhegazy mentioned this pull request Nov 30, 2016
@guncha
Copy link

@guncha guncha commented Nov 30, 2016

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 free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants