-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Infer IndexedAccess type when accessing property of type variable #30938
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
I guess this is a fallout of the new behaviour that ignores index signatures on constrained variables when assigning to? It feels like this should work, but would require deferring the simplification of |
This is indeed caused by #30769. A simpler example that exhibits the same issue: function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void) {
cb(param[0]); // Error
} The key scenario we want to handle is this: function fn3<T extends { [key: number]: string }>(param: T, cb: (element: T[number]) => void) {
cb(param[0]); // Error
} Here, the only thing that the constraint validates is that the numerically named properties of Now, the constraint Maybe the new rule should be in effect only when the target has an index signature and no other members. |
On further reflection, I think the right approach here is to only ignore string index signatures coming from constraints (as opposed to ignoring both string and numeric index signatures). The examples motivating the new rule all had string index signatures and really all were related to dictionary-like uses of objects. |
TypeScript Version: 3.5.0-dev.20190413
Search Terms:
Code
nightly version 20190413 (probably #30769?) broke code that looked like this
Expected behavior:
Passes type checking, type of
param.elements[0]
is inferred asT['elements'][number]
.Actual behavior:
Related Issues:
#30769
The text was updated successfully, but these errors were encountered: