A rest element must be last in a tuple type

Viewed 4614

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.
2 Answers

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.

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)[],
    ]
Related