I'm working on a Typescript function that can take a variable number of arguments and access them using the arguments object
:
function processArguments(): string {
const result: Array<string> = [];
for (const arg of arguments) {
const argType = typeof arg;
if (argType === 'string' || argType === 'number') {
result.push(arg.toString());
} else if (Array.isArray(arg)) {
if (arg.length) {
const inner = processArguments.apply(null, arg);
result.push(inner);
}
}
}
return result.join(' ');
}
When I try to use
const inner = processArguments.apply(null, arg);
, I encounter this error:
TS2345: Argument of type 'any[]' is not assignable to parameter of type '[]'. Target allows only 0 element(s) but source may have more.
I understand the error message, but I'm unsure of how to fix or properly annotate it. Any assistance would be greatly appreciated.