You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nested Promise not automatically unwrappedΒ #59111
typeSomeNode=string|Promise<string>asyncfunctionrenderChild(): Promise<SomeNode>{returnPromise.resolve("test")}functionrenderApp(children: SomeNode){}// Should typecheck but errors:// Type 'Promise<string>' is not assignable to type 'string'.(2345)renderApp(renderChild())// ^?
π Actual behavior
Argument of type'Promise<SomeNode>' is not assignable to parameter of type'SomeNode'.
Type 'Promise<SomeNode>' is not assignable to type'Promise<string>'.
Type 'SomeNode' is not assignable to type'string'.
Type 'Promise<string>' is not assignable to type'string'.(2345)
input.tsx(13, 11): Did you forget to use 'await'?
π Expected behavior
No typechecking error
Additional information about the issue
Nodes in React are typed as type Node = AwaitedNodes | Promise<AwaitedNodes>. However, the return type of async Components is Promise<Node>. But TypeScript will not consider this a Component because Promise<Node> is not assignable to Node even though it should be since at runtime Promise<Promise<T>> will never be observable and always collapse to Promise<T>.
Users can either fix this by using an unwieldy Promise<Awaited<ReactNode>>.
We can also fix this at the type level by allowing Node | Promise<Node> as the return type. Though I suspect this just pushes the issue one Promise-wrapping level away when TypeScript could collapse wrapped Promises automatically.
We can also fix this at the type level by allowing Node | Promise<Node> as the return type.
I think this is probably what I'd recommend. I believe Ron Buckton (@rbuckton) did explore something where Promise<T> could be compatible with T | Promise<T>; but I don't know if it would have handled the more recursive case you have up there.
You can't actually have a Promise<Promise<T>> in JS, but we have no mechanism to recursively unwrap a type parameter on instantiation. I looked into this a few years ago in #37615 (and #37540), but that solution depended on introducing magic inference rules for Promise-likes that couldn't be applied generally to other types.
Type 'ReactNode | Promise<ReactNode>' is not assignable to type 'ReactNode'.
Type 'Promise<ReactNode>' is not assignable to type 'ReactNode'.
Type 'Promise<ReactNode>' is not assignable to type 'Promise<AwaitedReactNode>'.
Type 'ReactNode' is not assignable to type 'AwaitedReactNode'.
Type 'Promise<AwaitedReactNode>' is not assignable to type 'AwaitedReactNode'.(2322)
const P: React.FunctionComponent
(props: {}) => ReactNode | Promise<ReactNode>
Since the type Promise<Promise<T>> cannot exist in javascript, it makes Promise a special case that would justify custom logic to forbid it IMHO. However, unlike the initial problem that arises in React, the join function is useless so this side of the problem is probably not worth considering.
Activity
DanielRosenwasser commentedon Jul 2, 2024
I think this is probably what I'd recommend. I believe Ron Buckton (@rbuckton) did explore something where
Promise<T>could be compatible withT | Promise<T>; but I don't know if it would have handled the more recursive case you have up there.rbuckton commentedon Jul 9, 2024
You can't actually have a
Promise<Promise<T>>in JS, but we have no mechanism to recursively unwrap a type parameter on instantiation. I looked into this a few years ago in #37615 (and #37540), but that solution depended on introducing magic inference rules forPromise-likes that couldn't be applied generally to other types.eps1lon commentedon Dec 9, 2024
Going with this approach for now (DefinitelyTyped/DefinitelyTyped#71379).
mzuzcak-ataccama commentedon Apr 1, 2025
Sebastian "Sebbie" Silbermann (@eps1lon) i believe that with your change, you broke the types in this kind of code
this is the error on
Pinvocation:Chromosore commentedon Aug 21, 2025
Kind of related (the other side of the problem) : this piece of code passes type check but does not work actually :
Since the type
Promise<Promise<T>>cannot exist in javascript, it makesPromisea special case that would justify custom logic to forbid it IMHO. However, unlike the initial problem that arises in React, thejoinfunction is useless so this side of the problem is probably not worth considering.