Skip to content

Error when attempting to inline destructure rest parameters #29404

@jonrimmer

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions