Is there a way to properly define dynamic strongly typed rest parameters using TypeScript 3.2? Let's consider the following scenario:
function execute<T, Params extends ICommandParametersMapping, Command extends keyof Params, Args extends Params[Command]>(command: Command, ...rest: Args): Args{
return;
}
execute('cmd2', true, 1, 'hello');
interface ICommandParametersMapping {
['cmd1']: [string];
['cmd2']: [boolean, number, string];
['cmd3']: [boolean, boolean];
}
Initially, everything appears to function correctly.
When passing arguments to execute
with the command cmd2
, TypeScript provides type information for the 3 arguments and the return value is also accurate...
However, an issue arises when declaring ...rest: Args
as the rest parameter.
The error message received is:
A rest parameter must be of an array type.