'bigint' is not comparable to 'number' with loose equality
See original GitHub issueTypeScript Version: 3.3.3
Search Terms: bigin == number not comparable loose equality
Code
1n == 1
error:
This condition will always return 'false' since the types 'bigint' and 'number' have no overlap.
but in chrome 72.0.3626.121
> 1n == 1
< true
and in MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
And similar issues
let x = [1, 2, 3]
x[1n]
error:
Type '1n' cannot be used as an index type.
in Chrome
> let x = [1, 2, 3]
x[1n]
< 2
Playground Link:
http://www.typescriptlang.org/play/index.html#src=1n %3D%3D 1
http://www.typescriptlang.org/play/index.html#src=let x %3D [1%2C 2%2C 3]
x[1n]
Related Issues: https://github.com/Microsoft/TypeScript/issues/30655
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Equality comparisons and sameness - JavaScript | MDN
BigInt : return true only if both operands have the same value. Symbol: return true only if both operands reference the same symbol....
Read more >Why does Number("x") == BigInt("x") ... only sometimes?
Your question boils down to BigInt(String(x)) ≟ BigInt(x) , which might assumed to be an equality for integer numbers x but is in...
Read more >BigInt Vs Number in JavaScript - TekTutorialsHub
BigInt is an integer, number is a decimal · They are implemented differently · BigInt can handle large numbers · BigInt is more...
Read more >The Essential Guide To JavaScript's Newest Data Type: BigInt
In JavaScript, the Number type cannot safely represent integer values larger than 2⁵³. This limitation has forced developers to use ...
Read more >Understanding Loose Equality In JavaScript
Conclusion · null equals undefined · Number(string) == number · BigInt(string) == bigint · Number(boolean) == anything · ToPrimitive(object) == ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
TS may rightly want to error on people using
==
at all. But… if someone uses==
instead of===
, then TS should accurately make its assumptions about what==
will do. It appears from this thread (and others) that’s not the case. Wanting to throw useful errors is fine, but incorrectly representing how JS works is not.IOW, the vastly more common perspective (much to my chagrin) is that devs default to using
===
, and that’s ESPECIALLY true for those who use type systems like TS. So, if someone is going out of their way to use TS, and they choose a==
, and that’s allowed by their linter/settings, then the assumption should be they intended to do that, and that they intended to take advantage of the coercive capabilities of==
.The interesting thing about comparing numbers with bigints in JavaScript is that it is not implemented by coercing both operands to numbers, nor is it implemented by coercing both arguments to bigints. Instead, the exact real values are compared. Here are some examples:
These values are different:
But they become equal when coerced to numbers:
These values can be compared:
But cannot be coerced to bigints:
Currently, it is not possible to get this comparison semantics in TypeScript without using type assertions. I agree that
==
operator shouldn’t have the same strict type checking rules as===
operator, as the use of the former signals the intent that the operands may be of different types.