-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Distributed keyof over union leads to erroneous indexed access #49000
Comments
This is working as intended. The conditional type |
@MartinJohns I know that |
But why shouldn't it be allowed? It's a legit operation. The result is the union of each type. Reading the properties on that type is fine. But yeah, writing to the properties results in unsound behaviour. But that's basically the same as when you assign a variable to |
It is not a legit operation. For example you cannot index the first element, |
Guess I was wrong. :-D To me it looks legit, but I get what you refer to. |
I'm not going to promise that fixing this won't create worse problems and ultimately lead to us declaring this "design limitation", but I agree that there should be an error here type KeyOf<T> = T extends unknown ? keyof T : never;
type Wrong<T> = { [K in KeyOf<T>]: T[K] }
// ^^^^ because the in-principle equivalent substitution of type S = { prop0: string } | { prop1: number; prop2: boolean } | { prop3: string[] };
type Temp = KeyOf<S>;
type GoodWrong = { [K in Temp]: S[K] }; produces an error |
@RyanCavanaugh Maybe it's just an oversight but the problem is with |
Oops, that was a bad copy/paste. Edited to fix. |
Hi there, I'd like to take a shot on this issue! |
@RyanCavanaugh |
Hi @RyanCavanaugh, @jfet97, @MartinJohns, I created a PR that seem to fix this error. The problem (paste again below) ...
... seems to be that in It seems to be a legit operation if e.g. So my working solution is to guard against return I place the logic at However, I am quite new to this and I am not quite sure:
Please do give me a few pointers whether I am approaching this correctly. Please do correct me if I am wrong! Thank you for reviewing! |
Bug Report
π Version & Regression Information
This is the behavior in every version I tried
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
It is accepted by the type checker, but it shouldn't be allowed because
T
could be a union of objects with unrelated properties. The computed type doesn't even have any sense:π Expected behavior
K
shouldn't be allowed to index typeT
.The text was updated successfully, but these errors were encountered: