I am having trouble understanding an inference error. The ts linter underline (event: E) of useCallback with the message bellow.
When I cast the callback of useCallback with as T the linter message is gone. Is there any way to avoid doing this? What did I didn't understand?
Complete code:
import { DependencyList, useCallback } from 'react'
const useStopBubblingCallback = <T extends (event: E) => any, E extends Event = Event>(
callback: T,
deps: DependencyList
) : T =>
useCallback<T>((event: E) => {
event.stopPropagation()
event.preventDefault()
return callback(event)
}, deps)
export default useStopBubblingCallback
Linter message:
Argument of type '(event: E) => any' is not assignable to parameter of type 'T'.
'(event: E) => any' is assignable to the constraint of type 'T',
but 'T' could be instantiated with a different subtype of constraint
'(event: E) => any'.ts(2345)