Skip to content

T[keyof T] assignable to T[K] #17166

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
olegdunkan opened this issue Jul 13, 2017 · 3 comments · Fixed by #17912
Closed

T[keyof T] assignable to T[K] #17166

olegdunkan opened this issue Jul 13, 2017 · 3 comments · Fixed by #17912
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@olegdunkan
Copy link

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
function f<T, K extends keyof T>(obj: T, k: K, value:T[K]) {
  for (let key in obj) {
    value = obj[key];
    break;    
  } 
  return value;
}

f({ x: 1, y: "" }, 'y', "1").charAt(0); //runtime error

Expected behavior:
Compiler error
Actual behavior:
Runtime error

@jcalz
Copy link
Contributor

jcalz commented Jul 13, 2017

It looks like TS is interpreting T[K] where K extends keyof T to be something like T[keyof T], i.e., the union of all values of properties of T. And so it allows any value assignable to T[keyof T] to a variable of type T[K], even when that could lead to runtime errors.

Another instance of this:

interface Foo {    
    s: string;
    n: number;
}

function g<K extends keyof Foo>() {
    var indexed: Foo[K] = 'what';   // no error? what if K is 'n'?
}

I doubt this is intended. It seems like a design limitation, if not a bug.

@olegdunkan
Copy link
Author

olegdunkan commented Jul 13, 2017

@jcalz I am also in doubt about this, because there is no specification for this case all I can is to pretend it to be a bug)
But my thoughts about this case are other.
TypeScript can infer type for value as string, but type of obj[key] maybe union of all T[K] types therefore we can't assign union of type to it's constituent. But it is a assumption.

@ghost ghost changed the title Lookup type inside "for in" loop T[keyof T] assignable to T[K] Jul 13, 2017
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Aug 27, 2017
@sandersn
Copy link
Member

@olegdunkan the fix for your bug is up at #17912.

@jcalz your repro exposes a design limitation; if we distinguished the types of read locations and write locations then we could say that the type of Foo[K] is string | number when reading and string & number when writing.

However, read/write distinction is similar to variance handling, so we are unlikely to add one set of features without the other. And that set of features is very complex, both to use and to implement, so I don't think it will happen soon, if at all.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 10, 2018
@mhegazy mhegazy added this to the TypeScript 2.7.1 milestone Jan 10, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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

Successfully merging a pull request may close this issue.

5 participants