Closed
Description
TypeScript Version: playground
Code
var a: "x" | ("y" & "z");
var b: {x:number, y:string, z:boolean};
var c: keyof typeof b;
c = a; //ok
var x:typeof b[typeof a];
Expected behavior:
An indexed access type T[K] requires K to be a type that is assignable to keyof T
...
T[K] is resolved as follows:If K is a union type K1 | K2 | ... | Kn, T[K] is equivalent to T[K1] | T[K2] | ... | T[Kn]
I expected the type of x
to be number
, because "x" | ("y" & "z")
is assignable to keyof {x:number, y:string, z:boolean}
T["x" | ("y" & "z")] = T["x"] | T["y" & "z"] = number | ? (never or empty)
but result of union is the number
type;
Edited:
In other words I expected T["y" & "z"]
to be never
Actual behavior:
error - Type '"y" & "z"' cannot be used as an index type