Skip to content

Premature resolution of conditional type depending on generic key #41613

@jcalz

Description

@jcalz

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions