microsoft / microsoft/TypeScript

Continuing #17110 - Given generic `W extends { x: A }` should be able to use `keyof W['x']` to index w.x

Open
#33,181 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: typescript@3.7.0-dev.20190831

Search Terms: tagged disjoint union generic

This issue stems from a question I asked on StackOverflow that @jcalz generously researched for me. I adapted his example to be a closer parallel to the example given in #17110.

Code

interface A { a: string; }

interface W {
    x: A
}

function foo<W1 extends W>(w: W1, k: keyof W1['x']) {
  w.x[k]; // Type 'keyof W1["x"]' cannot be used to index type 'A'.

  const x: W1['x'] = w.x; // Okay
  x[k] // Okay
}

Expected behavior:

Same as #17110. However, unlike #17110, W['x'] is only A instead of A | B | undefined. The explanation given as to why #17110 was not a bug was:

The constraint of keyof W1["x"] is keyof (A | B), which simplifies to keyof A & keyof B, which is never (because there are no common properties).

However, here keyof W1['x'] is keyof A which is not never.

Actual behavior:

Same as #17110.

Playground Link: http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgILIN7LgLmQZzClAHMBuZAXwChrRJZEUB1Ta5D5ADz1WpuowAriARhgAexDIYEiQB5mARmQQukEABN8yZgD4AFAHc8ygDTIA1nksQAnhJi6lAbQDkXNwF0AlGw5GAHRcLpZeFAD0EcgAKnYADihutg5Oyi4ARFwZXm7ICHAgIBJgyABGKEL4EJrIYBLIoJpqdQlJqG6BtBwIUoTcpq4e3sgAvMhBXJHRAPKWcHbs3KFeyFHIcwv8QA

Related Issues: #17110

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generic indexing example with typescript@3.7.0-dev.20190831 or TypeScript@next, using the linked Playground and comparing the expected and actual behavior. Read related issue #17110 for the existing reasoning; done means the reported access is accepted without regressing the related union case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.