A rest element must be last in a tuple type
Asked Answered
R

2

7

When writing type for cb:

const fn1 = (
  cb: (...args: [...args: any[], fn2: () => string, fn3: () => string]) => any,
  ...args: any[]
) => {
  const fn2 = () => 'fn2'
  const fn3 = () => 'fn3'
  cb(...args, fn2, fn3)
}

Run tsc, an error is thrown:

A rest element must be last in a tuple type.
Ribbonwood answered 12/3, 2021 at 3:37 Comment(2)
This would be a good use case for the better “rest” tuple typing in TS4.2: devblogs.microsoft.com/typescript/announcing-typescript-4-2/…Magdalenmagdalena
Is this what you're looking for? It's hard to tell without some examples for what you want to be able to do inside and outside the function.Cytology
R
13

It turns out I was using the global tsc the whole time, which was v3.2.9, no wonder I had behaviors different from other people's. When I switched to the local tsc which was v4.2.3, the code was compiled successfully.

Ribbonwood answered 12/3, 2021 at 9:12 Comment(1)
when i run tsc -v i get Version 4.2.3 yet when tsc it runs fine when i run npm run build which executes tsc; cp package.json ./lib/src/package.json i get A rest element must be last in a tuple type. Npm update fixed everything.Hadron
S
0

Just like the error says move, "...(boolean | ExpressionInputType | ExpressionSpecification)[]" element at last position

"case",
        boolean | ExpressionSpecification,
        ...(boolean | ExpressionInputType | ExpressionSpecification)[],
        ExpressionInputType | ExpressionSpecification,
        ExpressionInputType | ExpressionSpecification,
    ]

Like this

"case",
        boolean | ExpressionSpecification,
        ExpressionInputType | ExpressionSpecification,
        ExpressionInputType | ExpressionSpecification,
        ...(boolean | ExpressionInputType | ExpressionSpecification)[],
    ]
Shivery answered 2/9, 2022 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.