I am trying to tell my function to accept either a string or a custom type
onPress: (value: string | CustomType)=>void
however when I assign it a string or CustomType the compiler yells at me with is not assignable to type '(value: string | CustomType ) => void'
How can I fix it?
It seems to me, that you are mistaken regarding the type of your
onPressvariable. It is not of typestring | CustomType, but of type(value: string | CustomType) => void. That type represents a function that takes a single argument of typestringorCustomTypeand does not return anything.You could assign an existing function to
onPress:Or you could assign it an anonymous function:
Or you could assign it an arrow function (also anonymous):