Skip to content

Issues with parameterizing over a union type #13073

Closed
@johnfn

Description

@johnfn

Why does this code work:

type KeyTypes = "a" | "b"
let MyThingy: { [key in KeyTypes]: string[] };

function addToMyThingy(key: "a" | "b") {
    MyThingy[key].push("a");
}

And this code fail?

type KeyTypes = "a" | "b"
let MyThingy: { [key in KeyTypes]: string[] };

function addToMyThingy<S extends KeyTypes>(key: S) {
    MyThingy[key].push("a"); // Error: Property 'push' does not exist on type '{ a: string[]; b: string[]; }[S]'.
}

Unless I've misunderstood S extends KeyTypes, all I've done is constrained key to be "a" or "b" with my type parameter.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions