Skip to content

T[K] not assignable to Partial<T[K]> #18201

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

Closed
krishan opened this issue Sep 1, 2017 · 2 comments
Closed

T[K] not assignable to Partial<T[K]> #18201

krishan opened this issue Sep 1, 2017 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@krishan
Copy link

krishan commented Sep 1, 2017

TypeScript Version: 2.5.2

Code

function f<T, K extends keyof T>(t: T, k: K): Partial<T[K]> {
  return t[k];
}

Expected behavior:
No error. In my understanding, X should always be assignable to Partial<X>.

Actual behavior:

TS2322: Type 'T[K]' is not assignable to type 'Partial<T[K]>'.
  Type 'T[string]' is not assignable to type 'Partial<T[K]>'.
@krishan
Copy link
Author

krishan commented Sep 1, 2017

If I break down the above code into two separate functions, there's no error. Therefore I think this must be a bug.

function f<T, K extends keyof T>(t: T, k: K): Partial<T[K]> {
  return g(h(t, k));
}

function h<T, K extends keyof T>(t: T, k: K): T[K] {
  return t[k];
}

function g<T>(t: T): Partial<T> {
  return t;
}

@DanielRosenwasser
Copy link
Member

This may be related to #18156.

@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 5, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Sep 5, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.6, TypeScript 2.7 Oct 9, 2017
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Oct 29, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

4 participants