3

I'm running Typescript inside of VSCode and this line

const Felix_Reverse = ([h, ...r]) => r.length > 0 && Felix_Reverse(r) + h || h;

throws the following error with the r parameter to the embedded call to Felix_Reverse highlighted

Argument of type 'any[]' is not assignable to parameter of type '[any, ...any[]]'. 
    Property '0' is missing in type 'any[]' but required in type '[any, ...any[]]'.

What does this mean and how should I properly annotate the function?

3
  • 1
    I think you need to specify type of h and r
    – Tien Duong
    Jul 29, 2019 at 9:35
  • 1
    I have read this error message several times and I have even less of a clue than when I hadn't. What you have should work. I guess it does in JS, but TS complains for some reason. It doesn't seem to like the destructuring would be my best guess.
    – VLAZ
    Jul 29, 2019 at 9:37
  • Thats because r.length > 0 does not assert that r is a non empty array. Destructuring however requires that (otherwise h could be undefined) Jul 29, 2019 at 9:58

2 Answers 2

3

You need to specify the argument types and return type:

const Felix_Reverse = ([h, ...r]: number[]): number =>
  r.length > 0 && Felix_Reverse(r) + h || h;

In that situation, h will be of type number and r of type number[]

2
  • number[] would be more appropriate. Jul 29, 2019 at 9:58
  • or string[], depends on the usecase. Jul 29, 2019 at 10:02
2

The underlying problem here is that array destructuring serves two different purposes: One is to pass values like a tuple (e.g. React returns such a tuple from useState), on the other hand it is used to access specific parts of an array like you did. Typescript has to type both purposes correctly, thats why it types the array destructured parameter as a tuple type by default:

  const Felix_Reverse = ([t, ...h]: [any, ...any[]]) => /*...*/;

That's great on the one hand, because it prevents:

  Felix_Reverse([])

as that does not fullfill the tuple type (as that has to have a first entry of type any). Now thats exactly what TS complains about, it says Property '0' is missing in type 'any[]' but required in type '[any, ...any[]]'., or in other words: It cannot guarantee that the recursive call has at least one element (actually it could, because of r.length > 0, but the compiler isn't that clever [yet]).

Now to resolve that, we have to turn the tuple type into an array type. Through that, even empty arrays can be passed, however then t might be undefined:

 const Felix_Reverse = ([h, ...r]: string[] | number[]) => r.length > 0 && Felix_Reverse(r) + h || h;
1
  • 1
    Great explanation, thanks. I was really confused what was happening. I completely missed that r.length would be the culprit here as the compiler tends to have problem with indirect assertions "r.length > 0 therefore Felix_Reverse(r) accepts a non-empty array"
    – VLAZ
    Jul 29, 2019 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.