Skip to content

Distribute indexes of indexed access types first#27243

Merged
weswigham merged 2 commits intomicrosoft:masterfrom
weswigham:distribute-indexes-first
Sep 21, 2018
Merged

Distribute indexes of indexed access types first#27243
weswigham merged 2 commits intomicrosoft:masterfrom
weswigham:distribute-indexes-first

Conversation

@weswigham
Copy link
Copy Markdown
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 a user, RyanCavanaugh and ahejlsberg September 20, 2018 21:49
@weswigham
Copy link
Copy Markdown
Member Author

@typescript-bot test this

@typescript-bot
Copy link
Copy Markdown
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
Copy Markdown
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)
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
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.

3 participants