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

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

Closed
rkirov opened this issue Apr 4, 2018 · 2 comments
Closed

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

rkirov opened this issue Apr 4, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@rkirov
Copy link
Contributor

rkirov commented Apr 4, 2018

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.

@rkirov rkirov changed the title Unclear what is the type T[keyof T] for a generic T is assignable to Unclear what is T[keyof T] for a generic T assignable to Apr 4, 2018
@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 4, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 4, 2018

It should be assignable to the top type, everything should (void excluded).

@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 4, 2018
@rkirov
Copy link
Contributor Author

rkirov commented Apr 4, 2018

github search for T[keyof T] has failed me, but accidentally I stumbled on #21368 which seems like a duplicate of this.

The issue #21368 is closed, but I feel it didn't reach its full resolution, so I am keeping this open until further clarification from TS team.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Apr 29, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants