Lets say i have this generic function:
const myFn = <T>(p: {
a: (n: number) => T,
b: (o: T) => void,
}) => {
// ...
}
If i use myFn
with no parameter for function a
it works and the type of T
can be inferred from the return type of a
:
myFn({
a: () => ({ n: 0 }), // Parameter of a is ignored
b: o => { o.n }, // Works!
})
but if i want to use the parameter for function a
suddenly the type of T can not be inferred:
myFn({
a: i => ({ n: 0 }), // Parameter i is used
b: o => { o.n }, // Error at o: Object is of type 'unknown'.ts(2571)
})
Can someone explain this behavior? How can i fix this such that the type of T can be inferred from the return type of a?