Typescript: Error when attempting to inline destructure rest parameters

Created on 14 Jan 2019  路  4Comments  路  Source: microsoft/TypeScript


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:

Duplicate

Most helpful comment

Duplicate of #6275, #26017 is already up.

All 4 comments

Would this work?

type FooParams = Parameters<typeof foo>;
function bar(a: FooParams[0], b: FooParams[1]) {}

@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.

Duplicate of #6275, #26017 is already up.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uber5001 picture uber5001  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

jbondc picture jbondc  路  3Comments

wmaurer picture wmaurer  路  3Comments

weswigham picture weswigham  路  3Comments