Skip to content

Promise intersection with primitive results in wrong awaited type #48927

Closed
@Azarattum

Description

@Azarattum

Bug Report

When we make an intersection of a promise with a primitive type like Promise<T> & T (where T is primitive (non-object)), the resulting type from await (Promise<T> & T) is wrong (still contains a promise). This is clearly a bug since Awaited<Promise<T> & T> and (Promise<T> & T).then(x => {}) behave correctly (the result is only T). The issue is only observed when using the await keyword.

The types arrangement might seem arbitrary, but it has a valid use case which is provided in the playground link below.

🔎 Search Terms

PROMISE & PRIMITIVE, PROMISE INTERSECTION, AWAITED TYPE, INCONSITENT THEN AND AWAIT

🕗 Version & Regression Information

Tested only versions available on playground!

  • This changed between versions 4.5.5 and 4.7.0-dev.20220503. 4.4.4 and lower are unaffected by this bug.

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = Promise<number> & number;
const a = {} as A;

const b = await a;
type B = typeof b; // B should be "number", but it is "Promise<number> & number"!

a.then((c) => {
  type C = typeof c; // C is "number" (which is corrent)
});

type D = Awaited<A> // D is "number" (which is corrent)

🙁 Actual behavior

Type B still contains Promise<number> despite of the value being awaited.

🙂 Expected behavior

In the example above B has to be equal to C and D.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions