Skip to content

Generics: False negative on type constraint validation involving a conditional type #25413

Closed
@UselessPickles

Description

@UselessPickles

TypeScript Version: 2.9.2

Search Terms:
conditional type constraint

Code

declare class Model<M extends MR, MR extends {}> {
    // Compiler error: 
    // Type 'M[K]' does not satisfy the constraint 'K extends keyof MR ? MR[K] : M[K]'.
    public getField2<K extends keyof M>(): Field<M[K], K extends keyof MR ? MR[K] : M[K]>
}

declare class Field<T extends TR, TR> {
}

Expected behavior:
No compiler error.

Because M extends MR, and K extends keyof M:

  • If K extends keyof MR, then I expect M[K] to satisfy the constraint MR[K].
  • Else, I expect M[K] to satisfy the constraint M[K].
  • Therefore, it seems that M[K] should satisfy the constraint K extends keyof MR ? MR[K] : M[K]

Actual behavior:
Compiler error: Type 'M[K]' does not satisfy the constraint 'K extends keyof MR ? MR[K] : M[K]'.

Playground Link:
https://www.typescriptlang.org/play/#src=declare%20class%20Model%3CM%20extends%20MR%2C%20MR%20extends%20%7B%7D%3E%20%7B%0D%0A%20%20%20%20%2F%2F%20Compiler%20error%3A%20%0D%0A%20%20%20%20%2F%2F%20Type%20'M%5BK%5D'%20does%20not%20satisfy%20the%20constraint%20'K%20extends%20keyof%20MR%20%3F%20MR%5BK%5D%20%3A%20M%5BK%5D'.%0D%0A%20%20%20%20public%20getField2%3CK%20extends%20keyof%20M%3E()%3A%20Field%3CM%5BK%5D%2C%20K%20extends%20keyof%20MR%20%3F%20MR%5BK%5D%20%3A%20M%5BK%5D%3E%0D%0A%7D%0D%0A%0D%0Adeclare%20class%20Field%3CT%20extends%20TR%2C%20TR%3E%20%7B%0D%0A%7D

Related Issues:
Possibly?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions