Have this custom TouchableOpacity function component, but for the style prop I'm getting a TS error
import { StyleSheet, Pressable, PressableProps, GestureResponderEvent } from 'react-native';
export default function TouchableOpacity(props: PressableProps) {
const { style, onPress, children } = props;
return (
<Pressable
onPress={(event: GestureResponderEvent) => {
onPress?.(event);
}}
style={({ pressed }) => [style, pressed ? styles.buttonPressedOpacity : null]}>
{children}
</Pressable>
);
}
const styles = StyleSheet.create({
buttonPressedOpacity: {
opacity: 0.5,
},
});
Here is the complete TS complaint:
TS2322: Type '({ pressed }: PressableStateCallbackType) => (StyleProp | ((state: PressableStateCallbackType) => StyleProp<...>) | { ...; })[]' is not assignable to type 'StyleProp | ((state: PressableStateCallbackType) => StyleProp)'. Type '({ pressed }: PressableStateCallbackType) => (StyleProp | ((state: PressableStateCallbackType) => StyleProp<...>) | { ...; })[]' is not assignable to type '(state: PressableStateCallbackType) => StyleProp'. Type '(StyleProp | ((state: PressableStateCallbackType) => StyleProp) | { opacity: number; })[]' is not assignable to type 'StyleProp'. Type '(StyleProp | ((state: PressableStateCallbackType) => StyleProp) | { opacity: number; })[]' is not assignable to type 'RecursiveArray<ViewStyle | Falsy | RegisteredStyle>'. The types returned by 'pop()' are incompatible between these types. Type 'StyleProp | ((state: PressableStateCallbackType) => StyleProp) | { opacity: number; }' is not assignable to type 'ViewStyle | Falsy | RegisteredStyle | RecursiveArray<ViewStyle | Falsy | RegisteredStyle> | readonly (ViewStyle | ... 1 more ... | RegisteredStyle<...>)[]'. Type '(state: PressableStateCallbackType) => StyleProp' is not assignable to type 'ViewStyle | Falsy | RegisteredStyle | RecursiveArray<ViewStyle | Falsy | RegisteredStyle> | readonly (ViewStyle | ... 1 more ... | RegisteredStyle<...>)[]'. index.d.ts(542, 5): The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & PressableProps & RefAttributes'