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:
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.
Most helpful comment
Duplicate of #6275, #26017 is already up.