I'm trying to make undefined values be optional, while non-optional values are optional. My perfect example would be:
type TypeObj = {
a: undefined;
b: number;
c: string;
}
call("a"); call("b", 1); call("c", "3"); // should work
call("b", "a"); call("c", 3) // shouldn't work
call("b"); call("c") // also shouldn't work
but my current approach (with the second parameter being optional) allows calling b and c without the second parameter, which I do not want.
function call<K extends keyof TypeObj = keyof TypeObj>(key: K, param?: TypeObj[K]) {
}
call("a"); call("b", 1); call("c", "3"); // works (good)
call("b", "a"); call("c", 3) // doesn't work (good)
call("b"); call("c") // works (don't want that)