typescripttypestype-definition

A rest element must be last in a tuple type


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.

Solution

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