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

Isomorphic mapped types and unions #12447

Merged
merged 4 commits into from Nov 24, 2016
Merged

Isomorphic mapped types and unions #12447

merged 4 commits into from Nov 24, 2016

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Nov 22, 2016

A mapped type of the form { [P in keyof T]: X }, where T is some type parameter, is said to be an isomorphic mapped type because it produces a type with the same shape as T. With this PR we make instantiations of isomorphic mapped types distributive over union types. For example, when { [P in keyof T]: X } is instantiated with A | B for T, we produce { [P in keyof A]: X } | { [P in keyof B]: X }. Furthermore, when a primitive type is substituted for T in an isomorphic mapped type, we simply produce that primitive type. For example, when { [P in keyof T]: X } is instantiated with A | undefined for T, we produce { [P in keyof A]: X } | undefined.

type A = { a: string };
type B = { b: string };
type C = { c: string };

type T1 = Partial<A | B | C>;  // Partial<A> | Partial<B> | Partial<C>
type T2 = Readonly<A | B | null | undefined>;  // Readonly<A> | Readonly<B> | null | undefined
type T3 = Readonly<A | B[] | string>;  // Readonly<A> | Readonly<B[]> | string
type T4 = Readonly<string>;  // string
type T5 = Readonly<string | number | boolean>;  // string | number | boolean

This PR also includes a minor fix that makes any isomorphic mapped type assignable to an empty object type.

@ahejlsberg
Copy link
Member Author

@mhegazy Ping!

@mhegazy
Copy link
Contributor

mhegazy commented Nov 23, 2016

//cc: @DanielRosenwasser and @RyanCavanaugh

@ahejlsberg ahejlsberg merged commit e128add into master Nov 24, 2016
@ahejlsberg ahejlsberg deleted the mappedTypesAndUnions branch November 24, 2016 01:36
@mhegazy mhegazy mentioned this pull request Nov 24, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants