It appears that there is a misunderstanding regarding the data type of your variable onPress
. It is not simply a string | CustomType
, but rather a function of type
(value: string | CustomType) => void
. This signifies a function that accepts a single argument of either type
string
or
CustomType
, and does not return any value.
You have the option to assign an existing function to the onPress
variable:
type CustomType = number // used for testing
let onPress: (value: string | CustomType) => void
function doSomething(value: string | CustomType) {
console.log("Performing action with value " + value)
}
onPress = doSomething
onPress("BY432")
Alternatively, you can assign an anonymous function to it:
type CustomType = number // used for testing
let onPress: (value: string | CustomType) => void
onPress = function (value) {
console.log("Performing action with value " + value)
}
onPress("BY432")
Another option is to assign an arrow function (also anonymous):
type CustomType = number // used for testing
let onPress: (value: string | CustomType) => void
onPress = value => console.log("Performing action with value " + value)
onPress("BY432")