Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: onPress type

I just ported my react-native project to typescript and have a question about functions as props

im passing:

<DisplayCardsWithLikes
data={testData}
likes={500}
onPress={() => this.props.navigation.navigate("CardDetailScreen")}
/>

to

type Props = {
  onPress: Function
}


const FloatingActionButtonSimple = (props:Props) => {
  const {onPress} = props
  return (
    <View style={styles.containerFab}>
      <TouchableOpacity style={styles.fab} onPress={onPress}>
        <Icon name="plus" size={16} color={"white"} />
      </TouchableOpacity>
    </View>
  );
};

Error:

Error, caused by child onPress:
o overload matches this call.
  Overload 1 of 2, '(props: Readonly<TouchableOpacityProps>): TouchableOpacity', gave the following error.
    Type 'Function' is not assignable to type '(event: GestureResponderEvent) => void'.
      Type 'Function' provides no match for the signature '(event: GestureResponderEvent): void'.
  Overload 2 of 2, '(props: TouchableOpacityProps, context?: any): TouchableOpacity', gave the following error.
    Type 'Function' is not assignable to type '(event: GestureResponderEvent) => void'.ts(2769)
index.d.ts(5125, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Readonly<TouchableOpacityProps> & Readonly<...>'
index.d.ts(5125, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Readonly<TouchableOpacityProps> & Readonly<...>'

Summary: onPress passed as prop (is a function). On child type onPress:Function displays an error(the one above), onPress:any works though. I basically don't know which kind of type the onPress prop is

So nothing crazy, but if I define onPress as a function it displays an error, so apparently that's not the correct type. Do you know which type this onPress functions are?

Thanks a lot!

like image 311
Fredyonge Avatar asked Jan 24 '20 18:01

Fredyonge


2 Answers

You need to define type as below to get rid of type error from tslint:

type Props {
   onPress: (event: GestureResponderEvent) => void
}

OR

type Props {
   onPress(): void
}

OR

type Props {
   onPress(params: type): void
}
like image 71
Muhammad Mehar Avatar answered Oct 19 '22 20:10

Muhammad Mehar


That error message shows the type that it would accept for your function as this:

(event: GestureResponderEvent) => void

What's important to note is => void which means it's expecting a function that does not return a value.

But this function:

() => this.props.navigation.navigate("CardDetailScreen")

does return a value. Arrow function's without {}'s return the result of their expression.

The fix is to add {} to your callback so the function does not return anything, which will match the expected type:

onPress={() => { this.props.navigation.navigate("CardDetailScreen") } }
like image 9
Alex Wayne Avatar answered Oct 19 '22 20:10

Alex Wayne