Casting anonymous function parameters

548 views Asked by At

I've got a function inside of react (using typescript), where a container component is passing down variables to a child component. The variables are functions. However, when I'm passing these down, i get the "noImplicitAny" error. zoomIn and zoomOut are functions, and rest seems like an array. How should I assign a type to zoomIn, zoomOut, and ...rest?

<TransformWrapper>
  {({ zoomIn, zoomOut, ...rest }) => (
    <TransformComponent>
      <Something onClick={zoomIn} />
     <TransformComponent/>
   )}

1

There are 1 answers

0
see sharper On BEST ANSWER

The type of a function can be Function to match any function, but it's better to specify the parameter and return types like so:

myFunc: (p1: type1, p2: type2) => returnType

In the case of something like zoomIn it's probably () => void.

As for rest, you could set the type as any[], though there may be a better type - it's unclear from the limited code sample provided. Remember though that the error is 'no implicit any', meaning you can make it an explicit any if you want.