Skip to content

Error when attempting to inline destructure rest parameters #29404

Closed

Description

@jonrimmer

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms: rest parameters inline type destructure error

Code

function foo(...[a, b]) {
  return a + b;
}

Expected behavior:

The code is valid JavaScript and should compile without errors.

Actual behavior:

It errors:

index.ts:1:17 - error TS2501: A rest element cannot contain a binding pattern.

Why would you want to destructure rest params like this? In order to use Parameters<T> to type the parameters based on another function:

function foo(a: string, b: number) {}

function bar(...[a, b]: Parameters<typeof foo>) {}

Playground Link: https://www.typescriptlang.org/play/index.html#src=function%20foo(...%5Ba%2C%20b%5D)%20%7B%0D%0A%20%20%20%20return%20a%20%2B%20b%3B%0D%0A%7D%0D%0A

Related Issues:

Activity

  1. Kinrany commented on Jan 14, 2019

    @Kinrany

    Would this work?

    type FooParams = Parameters<typeof foo>;
    function bar(a: FooParams[0], b: FooParams[1]) {}
  2. jonrimmer commented on Jan 14, 2019

    @jonrimmer
    Author

    Ruslan Fadeev (@Kinrany) Yes, I think so. There are a few other workarounds. But none are as succinct, and given that it's valid JS, and TS is supposed to be a superset, I think this construction should work.

  3. weswigham commented on Jan 14, 2019

    @weswigham
    Member

    Duplicate of #6275, #26017 is already up.

  4. typescript-bot commented on Jan 17, 2019

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions