Closed
Description
π Search Terms
generics, generic loses genericness, generics with object types
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
β― Playground Link
π» Code
type M = {
a: { data: 'a' }
b: { data: 'b' }
c: { data: 'c' }
}
function n<O extends keyof M>(o: O): M[O]['data'] {
return 'a' // expected error
}
n('b')
π Actual behavior
It allows 'a', 'b' or 'c' instead of the generic type which is supposed to be the same as the input. The same problem occurs with assigning variables or functions inside the other function. Although the type must be M[O]['data']
and it says that it is that, it acts as if it were no longer generic, as in M[keyof M]['data']
.
π Expected behavior
I woud expect it to be M[O]['data']
.
Additional information about the issue
You don't have this issue if you remove the object with the data key and replace it with the value of data, for example ''a'
instead of { data: 'a'}