I have an api with a typed map from api name to api input object type:
type ApiInputTypes = {
doThis: { x: number }
doThat: { y: string }
}
type ApiNames = keyof ApiInputTypes
My goal is to write a generic function that can handle any of these api requests, but I'm getting a type error where I would not expect it.
function handleApi<Name extends ApiNames>(name: Name, args: ApiInputTypes[Name]) {
if (name === "doThis") {
args.x // Error
} else {
args.y // Error
}
}
Ironically, this still works though...
// But this works...
handleApi("doThis", { x: 190 })
Any ideas? Here's a playground link.