Problem

U must be an array. We know U is a value of P, but there is no guarantee that all values of P are arrays. That's because exec depends not on the concrete ICommandNameArgumentTypeMapping interface defined below, but on some P we don't fully know yet. And because we don't know it yet, we cannot trust it to follow the blueprint of ICommandNameArgumentTypeMapping โ€” after all, it might add some properties of its own which are not arrays.

Solution

The solution is to make sure all values โ€” present and future โ€” will always be arrays.

interface ICommandNameArgumentTypeMapping {
    ['cmd1']: [string];
    ['cmd2']: [boolean, number, string];
    ['cmd3']: [boolean, boolean];
    [index: string]: any[]
}

That extra property is called index signature.

You can, of course, be more precise here and say (string | number | boolean)[] instead of any[].

Bonus points

There are a few more mistakes in your code:

  • Computed property name cmd2 is duplicated
  • The T type parameter is unused
  • The P type parameter is incorrectly used (it is not used to describe parameters nor return types)
  • exec promises to return U, but it's returning undefined

The corrected solution:

function exec<P extends ICommandNameArgumentTypeMapping, E extends keyof P, U extends P[E]>(mapping: P, command: E, ...rest: U): U{
    return rest;
}

interface ICommandNameArgumentTypeMapping {
    ['cmd1']: [string];
    ['cmd2']: [boolean, number, string];
    ['cmd3']: [boolean, boolean];
    [index: string]: any[]
}

declare const mapping: ICommandNameArgumentTypeMapping;

exec(mapping, 'cmd2', true, 1, 'hello');
Answer from Karol Majewski on Stack Overflow
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ typescript โ€บ rest-parameters
TypeScript Rest Parameters
Learn about rest parameters in TypeScript. When the number of parameters that a function will receive is not known or can vary, we can use rest parameters.
๐ŸŒ
TypeScript Tutorial
typescripttutorial.net โ€บ home โ€บ typescript tutorial โ€บ typescript rest parameters
TypeScript Rest Parameters
July 11, 2024 - A rest parameter allows a function to accept zero or more arguments of the specified type. In TypeScript, the rest parameters follow these rules:
Discussions

Strongly typed rest parameters in TypeScript - Stack Overflow
How do I define dynamic strongly typed rest parameters with TypeScript 3.2? Here's my use case: function exec ( More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you validate the request body and query parameters?
I prefer to use yup More on reddit.com
๐ŸŒ r/node
6
1
July 21, 2022
Question: Is there a away to forward parameters from one function to another?
function bar(...args) { foo(...args) } should be all you need ...args in the signature collects every passed in parameter to a single array, and ...args in the call to foo spreads them into multiple parameters More on reddit.com
๐ŸŒ r/typescript
17
16
January 10, 2022
typing rest/spread props in react components

Typescript thinks you have a property called 'restProps'. I think what you want is

interface RestProps {}

function Box ({ position, ...restProps }: { position: number } & RestProps {
   ...
}
More on reddit.com
๐ŸŒ r/typescript
4
6
March 22, 2021
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ rest-parameters-in-typescript
Rest Parameters in TypeScript - GeeksforGeeks
January 22, 2025 - Rest parameters in TypeScript enable functions to handle an unlimited number of arguments by grouping them into an array. They are defined using ...
๐ŸŒ
typescriptlang.org
typescriptlang.org โ€บ docs โ€บ handbook โ€บ 2 โ€บ functions.html
TypeScript: Documentation - More on Functions
A rest parameter appears after all other parameters, and uses the ... syntax: ... In TypeScript, the type annotation on these parameters is implicitly any[] instead of any, and any type annotation given must be of the form Array or T[], or a tuple type (which weโ€™ll learn about later).
๐ŸŒ
Tektutorialshub
tektutorialshub.com โ€บ home โ€บ typescript โ€บ rest parameters in typescript
Rest Parameters in TypeScript - Tektutorialshub
March 15, 2023 - Rest Parameters in TypeScript allow us to accept a variable number of arguments as an array. TypeScript introduced this feature in ES6.
๐ŸŒ
GitBook
basarat.gitbook.io โ€บ typescript โ€บ future-javascript โ€บ rest-parameters
Rest Parameters | TypeScript Deep Dive
December 31, 2019 - Rest parameters (denoted by ...argumentName for the last argument) allow you to quickly accept multiple arguments in your function and get them as an array.
๐ŸŒ
Medium
medium.com โ€บ @alfmohenu โ€บ rest-and-spread-parameters-in-typescript-e98db065d518
Rest and Spread parameters in TypeScript. | by alfred a. mohenu | Medium
July 19, 2019 - Using the rest operator, we had to provide a parameter name ( restParam ) as part of our parameters and it had to be the last in order to capture the rest of the arguments that will be passed in.
Find elsewhere
Top answer
1 of 3
4

Problem

U must be an array. We know U is a value of P, but there is no guarantee that all values of P are arrays. That's because exec depends not on the concrete ICommandNameArgumentTypeMapping interface defined below, but on some P we don't fully know yet. And because we don't know it yet, we cannot trust it to follow the blueprint of ICommandNameArgumentTypeMapping โ€” after all, it might add some properties of its own which are not arrays.

Solution

The solution is to make sure all values โ€” present and future โ€” will always be arrays.

interface ICommandNameArgumentTypeMapping {
    ['cmd1']: [string];
    ['cmd2']: [boolean, number, string];
    ['cmd3']: [boolean, boolean];
    [index: string]: any[]
}

That extra property is called index signature.

You can, of course, be more precise here and say (string | number | boolean)[] instead of any[].

Bonus points

There are a few more mistakes in your code:

  • Computed property name cmd2 is duplicated
  • The T type parameter is unused
  • The P type parameter is incorrectly used (it is not used to describe parameters nor return types)
  • exec promises to return U, but it's returning undefined

The corrected solution:

function exec<P extends ICommandNameArgumentTypeMapping, E extends keyof P, U extends P[E]>(mapping: P, command: E, ...rest: U): U{
    return rest;
}

interface ICommandNameArgumentTypeMapping {
    ['cmd1']: [string];
    ['cmd2']: [boolean, number, string];
    ['cmd3']: [boolean, boolean];
    [index: string]: any[]
}

declare const mapping: ICommandNameArgumentTypeMapping;

exec(mapping, 'cmd2', true, 1, 'hello');
2 of 3
1

The problem is P extends ICommandNameArgumentTypeMapping, which means that exec() accepts any mapping that is a superset of the interface you've defined. This would allow non-array types. If you remove that constraint (and fix what I assume is a typo), you get no error messages.

interface ICommandNameArgumentTypeMapping {
    ['cmd1']: [string];
    ['cmd2']: [boolean, number, string];
    ['cmd3']: [boolean, boolean];
}

type P = ICommandNameArgumentTypeMapping;

function exec<T, E extends keyof P, U extends P[E]>(command: E, ...rest: U): U{
    return rest;
}

exec('cmd2', true, 1, 'hello');
๐ŸŒ
Cazton
cazton.com โ€บ blogs โ€บ technical โ€บ rest-parameters-in-typescript
REST Parameters in TypeScript | Cazton
In order to make sure our rest parameter accept strings, we need to define restDetails as string ...models:string[]) . Regardless of the data type that defines the rest parameter, it will always store as an array. Finally we make use of ES6's template string syntax to concatenate the product type and the model and finally log the output. Thus we can conclude that, in order to pass in multiple values as an input argument we make use of Rest parameters.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ typescript โ€บ typescript_rest_parameter.htm
TypeScript - The Rest Parameter
In TypeScript, a rest parameter allows a function to accept a variable number of arguments and store them as an array. This is useful when you want to define a function that can handle a variable number of arguments.
๐ŸŒ
Programiz
programiz.com โ€บ typescript โ€บ rest-parameters
TypeScript Rest Parameters
In TypeScript, rest parameters allow a function to accept an indefinite number of arguments as a single array. In this tutorial, you will learn about TypeScript rest parameters with the help of examples.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ typescript-rest-parameters-and-arguments
TypeScript Rest Parameters and Arguments - GeeksforGeeks
April 28, 2025 - A rest parameter allows us to pass zero or any number of arguments of the specified type to a function. In the function definition where we specify function parameters rest of the parameters should always come at last or the typescript compiler ...
๐ŸŒ
DEV Community
dev.to โ€บ this-is-learning โ€บ typescript-tips-tricks-rest-parameters-rest-arguments-4jl7
Typescript - Tips & Tricks - Rest Parameters & Rest Arguments - DEV Community
September 9, 2021 - Hi guy and welcome back :) Today I'll talk about the Rest Parameters and the Rest Arguments. Sometimes we need to write a simple code like this: function multiply(a: number, b: number): number { return a * b; } const result1 = multiply(2, 2); // 4 const result2 = multiply(2, 3); // 3 ยท but after a while the customer asks us to multiply for unbounded numbers, so we need to change our code. In these cases, typescript helps us with a feature called Rest Parameters.
๐ŸŒ
Run, Code & Learn
blog.delpuppo.net โ€บ typescript-tips-tricks-rest-parameters-rest-arguments
Typescript - Tips & Tricks - Rest Parameters & Rest Arguments
April 2, 2021 - but after a while the customer asks us to multiply for unbounded numbers, so we need to change our code. In these cases, typescript helps us with a feature called Rest Parameters.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ explain-about-rest-parameters-and-arguments-in-typescript
Explain about rest parameters and arguments in TypeScript
July 14, 2023 - The rest parameter is used to pass the multiple arguments to the function when we don't know how many arguments we need to pass to the function. The rest parameter name is written, followed by ??' (three dots).
๐ŸŒ
Gitbooks
hamednourhani.gitbooks.io โ€บ typescript-book โ€บ content โ€บ docs โ€บ rest-parameters.html
Rest Parameters ยท typescript-book - Hamed Nourhani
Rest parameters (denoted by ...argumentName for the last argument) allow you to quickly accept multiple arguments in your function and get them as an array.
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ guides โ€บ scaling-nodejs โ€บ rest-parameters-spread
Rest Parameters and Spread Syntax in TypeScript | Better Stack Community
November 27, 2025 - Rest parameters collect all remaining arguments into a typed array, enabling variadic functions with full type safety. The ... syntax before the last parameter tells TypeScript to gather all additional arguments into an array of the specified type.
๐ŸŒ
DevsCall
devscall.com โ€บ tutorials โ€บ learn-typescript โ€บ typescript-rest-parameters
Rest Parameters in TypeScript
Rest parameters in TypeScript allow a function to accept zero or more arguments of a specified type. They are represented as an array within the function, enabling the handling of multiple parameters. By using the ...
๐ŸŒ
Typescripttricks
typescripttricks.com โ€บ ts-rest-parameters
Understanding TypeScript Rest Parameters
May 14, 2025 - Rest parameters in TypeScript allow you to represent an indefinite number of arguments as an array. They are useful when you want a function to accept more arguments than the number of formally declared parameters.