I am in the process of creating a function type that is based on an existing utility type defining a mapping between keys and types:
type TypeMap = {
a: A;
b: B;
}
The goal is to construct a multi-signature function type where the key is used as a literal string in the first parameter:
type Result = {
(key: "a"): A;
(key: "b"): B;
}
Is it feasible to achieve this in TypeScript? I'm aware that function types and mapped types may not always work well together.
I can attempt the following approach, but it involves repeating the full list of keys which I'd like to avoid:
type TempFunc<K extends keyof TypeMap> = {
(key: K): TypeMap[K];
};
type Result = TempFunc<"a"> & TempFunc<"b">;
Note: This example is simplified; my actual TypeMap
comprises over 100 keys.