0leg
0leg

Reputation: 14154

Specify variable type passing function as a prop

Have a parent component and child components. The goal is to lift the state up from the children to the parent like it is described in the official react tutorial - https://reactjs.org/docs/lifting-state-up.html.

In TypeScript one supposed to define props interface, like this:

interface TemperatureInputInterface {
  ..
  this.props.onTemperatureChange: what_type_here?
}

But what type to use in this case, since all of them give smth like:

Cannot invoke an expression whose type lacks a call signature. Type 'what_type_here' has no compatible call signatures.

Upvotes: 0

Views: 138

Answers (2)

dashton
dashton

Reputation: 2714

You would just give it the types required by the callback, so:

interface TemperatureInputInterface {
   onTemperatureChange: (newValue: number) => void;
}

Upvotes: 2

Hemerson Carlin
Hemerson Carlin

Reputation: 7424

You can use, for example:

interface TemperatureChangeFunc {
    (arg1: string, arg2: number): boolean;
}

interface TemperatureInputInterface {
  onTemperatureChange: TemperatureChangeFunc,
}

or simply:

interface TemperatureInputInterface {
  onTemperatureChange: (arg1: string, arg2: number) => boolean,
}

Upvotes: 1

Related Questions