-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Index access type from intersection #17929
Comments
The compiler verifies that an index operation is legal. the way it does that is either the key is part of the known keys of the indexed type or constraint to it. in this case |
@mhegazy, thanks I know that complier works as intended, if we are looking at the sources. In my case we have union of types as index type, complier sees that first constituent |
at run-time an index with an unknown property would result in undefined. if the compiler did not limit the access on only known index types, every generic |
@mhegazy now I see, thanks a lot for the answer. |
|
@mhegazy yes, I know, it was my suggestion
the reason of my issue I want to simplify this: type A<T, K extends ("x"|"y") & keyof T> = T[K];
var a:A<{x:number, y:string, z:boolean}, "x"|"y">; //ok, type of a is number | string to this: type A<T> = T[("x"|"y") & keyof T];
var a:A<{x:number, y:string, z:boolean}>; //bad, type of a is any Because every time I instantiate Maybe there is some workaround to get it. |
TypeScript Version: playground
Code
Expected behavior:
#11929
I expected the type of
x
to benumber
, because"x" | ("y" & "z")
is assignable tokeyof {x:number, y:string, z:boolean}
T["x" | ("y" & "z")] = T["x"] | T["y" & "z"] = number | ? (never or empty)
but result of union is thenumber
type;Edited:
In other words I expected
T["y" & "z"]
to benever
Actual behavior:
error - Type '"y" & "z"' cannot be used as an index type
The text was updated successfully, but these errors were encountered: