I am working with typescript and passing a function into another function.
If I have a function that is passed into another function in typescript how should I write the type?
I have tried successHandler: function but this doesn't appear to work.
export function useSubscription(address: string, successHandler: function) {
successHandler(address)
}
Declare a type with a function signature and pass it around as the type:
type SuccessHandler = (address: string) => string;
function useSubscription(address: string, successHandler: SuccessHandler) {
successHandler(address)
}
You can declare it like this:
export function useSubscription(address: string, successHandler: (string) => void)) {
successHandler(address)
}
The change is with the capital F for Function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With