Skip to content

await/.then does not unwrap Promise<Promise<T>> #5582

@jamiebuilds

Description

@jamiebuilds

While it is possible to create types like Promise<Promise<T>>, await and Promise#then do not unwrap to T correctly the same way they do in JavaScript implementations.

[try flow]

declare function wrap<T>(fn: T): Promise<T>;

async function main() {
  let val: Promise<Promise<42>> = wrap(Promise.resolve(42));
  let res: 42 = await val;
  
  val.then((res: 42) => {
    // ...
  });
}

Error one: await

    7:   let res: 42 = await val;
                       ^ Promise. This type is incompatible with
    7:   let res: 42 = await val;
                  ^ number literal `42`

Error two: .then

    9:   val.then((res: 42) => {
                  ^ function. This type is incompatible with the expected param type of
    [LIB] static/v0.62.0/flowlib/core.js:598:       onFulfill?: (value: R) => Promise<U> | U,
                                                                ^ function type
        The first parameter is incompatible:
            6:   let val: Promise<Promise<42>> = wrap(Promise.resolve(42));
                                  ^ Promise. This type is incompatible with
            9:   val.then((res: 42) => {
                                ^ number literal `42`

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions