Closed
Description
TypeScript Version: [email protected]
Search Terms: TS80007 as of #32384
Code
type MaybePromise<T> = T | Promise<T>
type StatefulModifier<S> = MaybePromise<(arg: S) => number>
type CountActions<S = { count: number }> = Record<string,
(...args: any[]) => StatefulModifier<S>
>
const actions: CountActions = {
// works fine, `state` arg correctly inferred
increment: (toAdd: number) => {
return (state) => state.count + toAdd
},
// works fine, `state` arg correctly inferred from a promise.resolve res
first: () => Promise.resolve(mockedPromise()).then((res) => {
return (state) => state.count + res
}),
// works fine, `state` arg correctly inferred from a new promise
second: () => new Promise((res) => {
res((state) => state.count + 50)
}),
// fails, `state` arg as `any` and TS warning 80007 on async
third: async () => {
const res = await mockedPromise()
// arg type inferred as any
return (arg) => {
return 10 * res
}
},
}
Expected behavior:
The promised function signature of the third
object property should be inferred in the same respect as the previous two promised functions.
Actual behavior:
Typescript is able to infer the promised function signature when it's provided through => Promise.resolve
or => new Promise(...
but unable to do so when supplied as a result of an async function.
However, the inference resolves correctly when the MaybePromise
union type is just simply a promise:
type MaybePromise<T> = Promise<T>
Playground Link: Click me