-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
"Indexed access types which immediately index into a mapped object type" infering improvement is unsound #48730
Comments
I agree that |
I think that TS has always had unsoundness on writes to generic indices: function set<T, K extends keyof T>(obj: T, k: K, val: T[K]) {
obj[k] = val; // no compiler error, but unsound
}
const obj = { a: 0, b: "" }
set(obj, Math.random() < 2 ? "a" : "b", "");
obj.a.toFixed() // runtime error And presumably this is just another instance of that unsoundness? I guess I was naively assuming this wouldn't be the case, since one of the points of #30581 was to encode such correlations correctly (not a trivial task, as described in this comment). I'm not sure it's worth the refactoring involved in turning something into a distributive object type form as described in #47109 if you get behavior similar to using a type assertion. |
This was an intentional trade-off. If you're using the mapped type lookup pattern, you should use multiple type parameters if the value can appear in multiple places function processRecords<K1 extends keyof TypeMap, K2 extends keyof TypeMap>(record1: UnionRecord<K1>, record2: UnionRecord<K2>) {
// Errors as hoped
record1.f(record2.v);
} |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Version & Regression Information
When did you start seeing this bug occur? From
4.6
, in which indexed access inference was improved.β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The code typechecks, but it breaks at runtime:
TypeError: val.toUpperCase is not a function
.π Expected behavior
I think
record1.f(record2.v);
shouldn't be allowed.π Notes
I understand that the type parameter
K
isn't behaving like an existential type, so the correct way to typeprocessRecords
may be:Playground
I think that the main problem here is that the original definition:
may suggest that the two nodes are related, because this whole feature was added to express correlation between things. But that may not be the case.
And what about a more general one?
Playground
How could I express the fact that each record is related only to itself? I don't even know if its possible to properly type it, I think variadic type arguments would be needed because I need a variable number of
K
s, each of which unrelated to others. Not sure. The above typing lets me do bad things as you can see in the playground.I tried the following, but it is all broken:
Playground
The text was updated successfully, but these errors were encountered: