question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'bigint' is not comparable to 'number' with loose equality

See original GitHub issue

TypeScript 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 QQ截图20190322171720

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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
getifycommented, Mar 6, 2020

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 ==.

3reactions
abacabadabacabacommented, Dec 1, 2020

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:

> 10n**50n == 10**50
false

But they become equal when coerced to numbers:

> Number(10n**50n) == Number(10**50)
true

These values can be compared:

> 1n == 1.5
false

But cannot be coerced to bigints:

> BigInt(1n) == BigInt(1.5)
Uncaught:
RangeError: The number 1.5 cannot be converted to a BigInt because it is not an integer

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found