Skip to content

typescript passes number | Promise<number> as if it were number (or something like that) #52036

Closed
@azizghuloum

Description

@azizghuloum

Bug Report

Something is very weird that makes typescript pass Promise<number> as if it were a number when it is an object field. Typescript correctly recognizes that Promise<number> is not a number when it is a plain variable. Weird.

🔎 Search Terms

promise numeric

🕗 Version & Regression Information

I believe this has always existed in typescript. I have tried multiple versions on playground (from 3.3 up to 5). I did read the FAQs in https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs and found nothing related.

⏯ Playground Link

Playground link with relevant code

💻 Code

const n: number | Promise<number> = Promise.resolve(0);
console.log(n >= 0);  // Correctly rejected: Operator >= cannot be applied to Promise<number> and number.

const foo: {n: number | Promise<number>} = {
  n: Promise.resolve(0),
};
console.log(foo.n >= 0); // Incorrectly type checks.  Runtime result is bogus.

🙁 Actual behavior

The last expression passes the type checker even though it is clearly wrong.

🙂 Expected behavior

That the last expression is rejected with Operator >= cannot be applied to Promise<number> and number..

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions