Here is the code snippet I'm working on:
const functions={
top1: {
f1: () => 'string',
f2: (b: boolean, n: number) => 1
},
top2: {
f3: (b: boolean) => b
}
}
I am looking to define an apply
function as follows:
function apply (top: keyof typeof functions, functionName: string, inputs: any[]) {
return functions[top][functionName](...inputs)
}
This will allow me to output different values using console.log:
console.log(apply('top1', 'f1', [])); // 'string'
console.log(apply('top1', 'f2', [true, 23])); // 1
console.log(apply('top2', 'f3', [false])); // false
apply('top2', 'f3', [1]); // should throw a TS error
However, when in strict mode (--strict
), the following error occurs:
"Element implicitly has an 'any' type because type '...' has no index signature"
This issue arises since functionName
is defined as a string, not as a keyof typeof functions[section]
. How can I resolve this?