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

Distribute indexes of indexed access types first #27243

Merged
merged 2 commits into from
Sep 21, 2018

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Sep 20, 2018

Fixes #27224

This change is a small addition (plus a bunch of comments to illustrate the process) to getSimplifiedIndexAccessType. To begin, let me explain the original issue. The contextual typing issue reported boils down to this:

function g<
    K extends "a" | "b">(x: ({ a: string } & { b: string })[K], y: string) {
    x = y;
}

This assignment worked in 3.0, and it should continue to do so. However the distribution change broke that, and in master (and 3.1) this assignment fails. Why? This is why: We'd see ({ a: string } & { b: string })[K] and distribute K into the intersection, producing { a: string }[K] & { b: string }[K]. Then for each we would get its apparent type, which means its constraint. That produces { a: string }["a" | "b"] & { b: string }["a" | "b"], which in turn becomes ({ a: string }["a"] | { a: string }["b"]) & ({ b: string }["a"] | { b: string }["b"]), which again becomes (string | unknown) & (unknown | string) which is simply unknown (or for constraint calculation, no constraint - which is subtly different in that no constraint means it's not assignable).

This changes this in two ways:

  1. We distribute index types over the object type before distributing into the object type.
  2. We do not perform distributions over the inner object type until the index type is no longer instantiable (and therefore might becomes a union we would need to distribute).

This changes how the algebra of the above example proceeds. Starting with ({ a: string } & { b: string })[K] we do not simplify via distribution, then we get its constraint, ({ a: string } & { b: string })["a" | "b"]. This distributes into ({ a: string } & { b: string })["a"] | ({ a: string } & { b: string })["b"] which becomes ({ a: string }["a"] & { b: string }["a"]) | ({ a: string }["b"] & { b: string }["b"]) which becomes (string & unknown) | (unknown & string) which is now just string (as unknown is a noop in an intersection like never is in a union), which is our desired type.

@weswigham weswigham added this to the TypeScript 3.1 milestone Sep 20, 2018
@weswigham weswigham requested review from ahejlsberg, RyanCavanaugh and a user September 20, 2018 21:49
@weswigham
Copy link
Member Author

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 20, 2018

Heya @weswigham, I've started to run the extended test suite on this PR at 7cfd206. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member Author

RWC build failed because master had some unaccepted diffs (which I have now accepted since they were just better error locations). The diff between master and this PR was null, so RWC is unaffected by this change.

@weswigham weswigham merged commit 219bb44 into microsoft:master Sep 21, 2018
@weswigham weswigham deleted the distribute-indexes-first branch September 21, 2018 00:03
weswigham added a commit to weswigham/TypeScript that referenced this pull request Sep 21, 2018
RyanCavanaugh added a commit that referenced this pull request Sep 21, 2018
Distribute indexes of indexed access types first (#27243)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants