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

Premature resolution of conditional type depending on generic key #41613

Closed
jcalz opened this issue Nov 20, 2020 · 0 comments · Fixed by #41622
Closed

Premature resolution of conditional type depending on generic key #41613

jcalz opened this issue Nov 20, 2020 · 0 comments · Fixed by #41622
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@jcalz
Copy link
Contributor

jcalz commented Nov 20, 2020

TypeScript Version: 4.0.5 and 4.2.0-dev.20201120

Search Terms: conditional, generic, eager/premature/incorrect, reduction/resolution/collapse/simplification

Code

type What<K extends string> =
    { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;
// What<K> is eagerly simplified to false before instantiation of K
 
type Huh = What<"y"> // expected: true, actual: false. 

Expected behavior:
What<"y"> should evaluate to true, since { x: { y: 0, z: 1 } } extends { x: { y: 0 } }

Actual behavior:
What<"y"> evaluates to false; in face the definition of What<K> gets reduced to just false before K is ever instantiated (at least according to quickinfo)

Playground Link: Provided

Related Issues:
#39364 and #30152 both seem to be in the general category of "over-eager reduction of generic conditional type" but the particulars might not be the same


From this SO question. @ahejlsberg said this issue is not the same as #39364 so I should open a new issue.

A possible wrinkle: this problem does not seem to exist if we "unwrap" the outer object type:

type Okay<K extends string> =
    { y: 0, z: 1 } extends { [P in K]: 0 } ? true : false;
type AllRight = Okay<"y"> // true
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Nov 21, 2020
@ahejlsberg ahejlsberg self-assigned this Nov 21, 2020
@ahejlsberg ahejlsberg added this to the TypeScript 4.2.0 milestone Nov 21, 2020
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Nov 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants