Closed
Description
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.