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'
The issue here is the type of
stylewhich is received fromPressablePropsand its concatination with the function you provide to thestyleprop ofPressable. The type ofstyleis as follows:The style is either
StyleProp<ViewStyle>or a function that receives aPressableStateCallbackTypeas an input parameter.However, the type of the
styleprop ofPressableis as follows:Thus, the following would work:
Since the types match.
However, if you provide the function that receives
PressableStateCallbackTypeyourself and you want to combine this with an additional style, then you have two possibilities.You know that
styleis indeed the mentioned functionYou know that
styleis aStyleProp<ViewStyle>You do not know
Edit: Actually, if you know the type and it is your custom component, then you could change the typing of the props of
TouchableOpacity.