-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type hole: everything is assignable to element of generic indexable #54160
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
Comments
Hmm, it seems like TS is eagerly reducing |
Oddly, this does error: |
@typeholes Username checks out |
Essentially the problem is the same as the one here: // No error, but expected
const a = <T extends { foo: number | string }>(x: T): T["foo"] => 123
// string type, but number value
const b = a({ foo: 'str' }) In other words, it's not limited to tuples. It's actually about indexed accesses with non-generic index constraints that are concrete~ (index signatures are not prone to this) on generic objects. The code responsible for this is here |
Probably related #45335 |
This may be trickier than it first appears. Clearly we can tell that // Exhibit A
const a = <T extends { foo: number | string }>(x: T): T["foo"] => 123 is unsound and should be an error. But what about this variant? // Exhibit B
const b = <T extends { foo: number | string }>(x: T): T["foo"] => x["foo"] It's already established that returning a // Exhibit C
const c = <T extends { foo: number | string }, U extends { foo: number | string }>(x: T, y: U): T["foo"] => y["foo"] ...which is again unsound ( |
Clearly the only sound behavior in this case is to only allow expressions that are exactly of the form |
I found this issue after coming across code like this: const test = <T extends { a: unknown }>(x: T) => { x.a = 123 }
const x = { a: '' }
test(x)
const notString = x.a
// ^? const notString: string I think that's a symptom of the same underlying problem. EDIT: Or not, because this also (unsurprisingly) has the same issue. I guess writing to properties of inputs is just inherently unsafe. |
Bug Report
π Search Terms
generic tuple element assignable
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
It's possible to return everything
π Expected behavior
It's possible to return entities which only have
T[0]
typeThe text was updated successfully, but these errors were encountered: