Let's say I have a type that is any array of two items: a list of parameters and a function with arguments that matches the parameters. To make it easy, I've created a function that can infer the generic type.
type MapParamsToFunction<A extends any[]> = [[...params: A], (...args: A) => void]
function asMap<A extends any[]>(map: MapParamsToFunction<A>): MapParamsToFunction<A> {
return map;
}
asMap([[],() => {}]) // good
asMap([[0, "Hello World", 2], (a: number, b: string, c: number) => { }]); // good
asMap([[0, "Hello World", 2], (a: number, b: number, c: number) => { }]); // error
asMap([[0, "Hello World"], (a: number, b: string, c: number) => { }]); // error
No problem so far. Now, I want to take this and create a dictionary where each key can have a different list of parameters/arguments. However, I don't see a way to have TypeScript use a different generic on each key.
I've tried using any[] on the type, but it will not throw a type error if the parameters don't match the arguments.
type FunctionDictionary<T extends string> = {
[K in T]: MapParamsToFunction<any[]>
}
function asDictionary<T extends string>(dict: FunctionDictionary<T>): FunctionDictionary<T> {
return dict;
}
let obj = asDictionary({
"foo": [[0, "Hello World", 2], (a: number, b: number, c: number) => { }], // no type error
"bar": [["","",""], (a: string, b: string, c: string) => { }]
});
Is there a way to map this, so each argument can have it's own generic parameter list?
type FunctionDictionary<T extends string> = {
[K in T]: MapParamsToFunction<?> // <--- what goes here
}