Closed
Description
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.
.