Skip to content

Unclear what is T[keyof T] for a generic T assignable to #23133

@rkirov

Description

@rkirov

Search Terms:
T[keyof T]

Code

class Unbounded<T> {
  foo(x: T[keyof T]) {
    let y: {}|undefined|null = x;
  }
}

Expected behavior:
Not absolutely sure, but I would like to be able to reason about T[keyof T] in terms of the types I already known - either it should behave like {} | null | undefined or like never. Or maybe I shouldn't be allowed to write that type.

Actual behavior:
Type 'T[keyof T]' is not assignable to type '{} | null | undefined'.

We noticed this during upgrading TS2.6 to TS2.7. It appears in TS2.6 this assignment was allowed for if one wrote Bounded<T extends {}>, where as in 2.7 and 2.8 that makes no difference. As expected when then lower bound has actual keys, they are used.

P.S. I miss the TS spec.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions