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

property access on unioned objects and string indices #17361

Closed
KiaraGrouwstra opened this issue Jul 23, 2017 · 2 comments
Closed

property access on unioned objects and string indices #17361

KiaraGrouwstra opened this issue Jul 23, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KiaraGrouwstra
Copy link
Contributor

Although not originally mentioned in the laws of union types (#805), I would generally expect a union of types undergoing a transformation to be equivalent to the result of its constituents undergoing the same transformation (distributive law?).

TypeScript Version: 2.5.0-dev.20170626, presumably any version from the introduction of union types

Code

type x = ({ a: 1 } | { a: 2 })['a'];
// 1 | 2
type y = { [k: string]: 3 }['a'];
// 3
type z = ({ a: 1 } | { a: 2 } | { [k: string]: 3 })['a'];
// ^ Property 'a' does not exist on type ...

Expected behavior:
z: 1 | 2 | 3

Actual behavior:
Property 'a' does not exist on type ...

@KiaraGrouwstra
Copy link
Contributor Author

I've since found that distributivity is not generally guaranteed with unions. This would fail, for one, for operations such as checking whether a given union contains a certain key.

Nevertheless, I believe the above snippet to intuitively indicate a bug.

@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 23, 2017
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 12, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 2, 2018

Duplicate of #21141, should be fixed bu #25307

@mhegazy mhegazy closed this as completed Jul 2, 2018
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 2, 2018
@mhegazy mhegazy removed this from the TypeScript 3.0 milestone Jul 2, 2018
@mhegazy mhegazy removed the Bug A bug in TypeScript label Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants