So I have a dictionary with functions and the arguments of those functions, like this:
<code>type TypeFnDict = {
nameOne: {params: Parameters<typeof functionOne>, fn: typeof functionOne},
nameTwo: {params: Parameters<typeof functionTwo>, fn: typeof functionTwo}
}
const FunctionsDict = {
nameOne: functionOne,
nameTwo: functionTwo,
}
</code>
<code>type TypeFnDict = {
nameOne: {params: Parameters<typeof functionOne>, fn: typeof functionOne},
nameTwo: {params: Parameters<typeof functionTwo>, fn: typeof functionTwo}
}
const FunctionsDict = {
nameOne: functionOne,
nameTwo: functionTwo,
}
</code>
type TypeFnDict = {
nameOne: {params: Parameters<typeof functionOne>, fn: typeof functionOne},
nameTwo: {params: Parameters<typeof functionTwo>, fn: typeof functionTwo}
}
const FunctionsDict = {
nameOne: functionOne,
nameTwo: functionTwo,
}
And what I want is to have another function that accepts the name as a generic and the right parameter for that respective function, calls it and return the result, like this:
<code>createStuff = function<T extends keyof TypeFnDict>(functionName: T, functionParams: TypeFnDict[T]["fn"] ) {
return FunctionsDict[functionName](...functionParams);
}
</code>
<code>createStuff = function<T extends keyof TypeFnDict>(functionName: T, functionParams: TypeFnDict[T]["fn"] ) {
return FunctionsDict[functionName](...functionParams);
}
</code>
createStuff = function<T extends keyof TypeFnDict>(functionName: T, functionParams: TypeFnDict[T]["fn"] ) {
return FunctionsDict[functionName](...functionParams);
}
But this is not working, I get “A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)”.
I feel like I’m doing something very wrong here, and probably this is a commom mistake. Anyone knows how to do something like this?