Hi, I'm not sure if this is a bug or somehow working as intended.
I have a function that takes an object literal as an argument, the properties of which may be functions/methods. I want to infer the return type of one function and use it as an argument to another. The consumer should be able to use contextual typing in function parameters.
For example:
function foo<R>(arg: {
boz: (input: string) => R
bar: (arg: R) => void
}) {}
foo({
boz: (input) => {
return { xyzzy: "quux" }
},
// arg: { xyzzy: string }
bar: (arg) => {},
}
This works fine, however if I move bar before boz in the object literal, it infers R as unknown:
foo({
// arg: unknown
bar: (arg) => {},
boz: (input) => {
return { xyzzy: "quux" }
},
})
If I put bar first and remove the argument for boz it then infers correctly:
foo({
// arg: { xyzzy: string }
bar: (arg) => {},
boz: () => {
return { xyzzy: "quux" }
},
})
If I put bar first and explicitly type arg, it then tries to constrain the return type of boz to that of arg. Just trying to understand what's going on here.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.