Skip to content

Index access type from intersection  #17929

Closed
@olegdunkan

Description

@olegdunkan

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]

#11929

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions