I have a very simple function that takes two arguments - node object and async function that somehow processes given node and returns processed node which can be anything.
I would like to make it generic and infer type from async function. This is my try but TS is complaining and I don't really understand why.
// ensure that callback is of a correct type
type NodeCallback<H> = H extends (node: Node) => Promise<infer R> ? (node: Node) => Promise<R> : never
// retrieve return value
type NodeCallbackReturnValue<H> = H extends (node: Node) => Promise<infer R> ? R : never
const myAsyncFunction = <_, C>(node: Node, cb: NodeCallback<C>) => {
return cb(node)
}
myAsyncFunction(document, (node: Node) => Promise.resolve(node.nodeType))
Playground available here