You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for TC39 "BigInt: Arbitrary precision integers in JavaScript" proposal #15096
The proposal adds a new syntax for integer literals:
42n // This is an integer literal
Implicit conversions to/from numbers and integers and expressions with mixed operands are expressly disallowed:
42n + 1 // Throws TypeError
Type constructors are provided that wrap at specified widths:
Integer.asUintN(width, Integer): Wrap an Integer between 0 and 2**width-1
Integer.asIntN(width, Integer): Wrap an Integer between -2**(width-1) and 2**(width-1)-1
Integer.parseInt(string[, radix]): Analogous to Number.parseInt, to parse an Integer from a String in any base.
Though not stated in the spec, these constructors should theoretically hint to the VM when it's possible to use a native 64-bit integer type instead of a bignum, and could therefore possibly be used by TypeScript to implement int64 and uint64 types in addition to e.g. an integer type of arbitrary precision.
I know people have been asking for integers for quite some time (e.g. #195, #4639), but have been held back by lack of native support of an integer type in JavaScript itself. Now it seems this proposal is stage 2 (scratch that, stage 3!) and has multi-browser vendor backing, so perhaps the prerequisites are finally in place to make integers happen.
changed the title [-]Support for TC39 "Integer: Arbitrary precision integers in JavaScript" proposal[/-][+]Support for TC39 "BigInt: Arbitrary precision integers in JavaScript" proposal[/+]on Jun 2, 2017
Looks like the BigInt proposal has also hit stage 3 (tc39/proposal-bigint@1b4c7e6). Would be awesome to see TypeScript support.
Reacted by Tony Arcieri, Ian Copp, recurrence, Jason Morley, Kenneth Brubaker, Bnaya Peretz, Steven, Paulo Cesar, Michał Lytek, Diego Stratta and 3 more
Reacted by Tony Arcieri, recurrence, Patricio Palladino, Bnaya Peretz, Gady, Diego Stratta, 牛牛 and Niro
Personally looking forward to getting the feature available - only concern is the ducktape syntax, my hope is that typescript can help keep the syntax clean.
[1,2,3][{toString(){return '1'}}] also works. I'm not sure how indexing is specified in the language though, and whether that includes BigInt. If it doesn't, I'm not sure whether BigInt should be allowed.
Mark Wubben (@novemberborn) You have a more general suggestion.
Currently Array is defined as type accepting only number as index.
Your suggest to extend indexer type beyond number | string | bingint.
It means that indexer type now has to be allowed as: string | number | bigint
We considered this in #25886 and decided against adding bigint as an index type. See my comment: #25886 (comment). As Mark Wubben (@novemberborn) says, any value can be used as a key in JS because any value can be converted to a string. Really, string and symbol are the only true index types. number is included because Arrays and TypedArrays are meant to be indexed with numeric indices. Since number can store a 53-bit unsigned integer, it is sufficient to index anything stored in almost all modern address spaces (e.g. the 48-bit x86-64 address space). There is really no reason to use a bigint as a numeric index unless you have a sparse array (in which case an object should be used instead). You can always just convert the bigint to a number or string, which would be a valid index type.
Reacted by Franklin Yu, Bnaya Peretz, Trotyl Yu, NN, Reinis Ivanovs and Ghabriel Nunes
Activity
aluanhaddad commentedon Apr 10, 2017
🚲:house:
Why didn't they choose
Zfor the literal suffix?[-]Support for TC39 "Integer: Arbitrary precision integers in JavaScript" proposal[/-][+]Support for TC39 "BigInt: Arbitrary precision integers in JavaScript" proposal[/+]tarcieri commentedon Jul 11, 2017
Looks like this may be landing in Babel soon:
babel/proposals#2
tarcieri commentedon Jul 26, 2017
Initial (WIP) babel PR for this: babel/babel#6015
Cixelyn commentedon Aug 1, 2017
Looks like the BigInt proposal has also hit stage 3 (tc39/proposal-bigint@1b4c7e6). Would be awesome to see TypeScript support.
raix commentedon Jan 15, 2018
Personally looking forward to getting the feature available - only concern is the ducktape syntax, my hope is that typescript can help keep the syntax clean.
styfle commentedon Mar 7, 2018
Ryan Cavanaugh (@RyanCavanaugh) Mohamed Hegazy (@mhegazy) Andy (Andrewkraft) (@Andy-MS)
Are there plans to started implementing BigInt now that it's Stage 3?
Or does it need to land in Edge first?
Bnaya commentedon Mar 7, 2018
BigInt is implemented in V8, means we will have it in chrome and V8 soon
refs:
https://twitter.com/bmeurer/status/969656997879713792
samrg472 commentedon Jun 7, 2018
Node 10.4.0 ships with V8 6.7, a flag is no longer needed to be able to use big integers.
Kingwl commentedon Jun 8, 2018
seems have a plan to support it on 3.0
styfle commentedon Jun 8, 2018
Wenlu Wang (@Kingwl) Yes, I see it on the roadmap for 3.0 as you said 👍
For others viewing this issue, the roadmap can be found on the wiki here: https://github.com/Microsoft/TypeScript/wiki/Roadmap#30-july-2018
19 remaining items
gatsbyz commentedon Sep 20, 2018
Thanks for this!
NN--- commentedon Nov 14, 2018
What about indexer type ?
This code is valid JS:
It means that indexer type now has to be allowed as: string | number | bigint
novemberborn commentedon Nov 14, 2018
[1,2,3][{toString(){return '1'}}]also works. I'm not sure how indexing is specified in the language though, and whether that includesBigInt. If it doesn't, I'm not sure whetherBigIntshould be allowed.NN--- commentedon Nov 14, 2018
Mark Wubben (@novemberborn) You have a more general suggestion.
Currently Array is defined as type accepting only number as index.
Your suggest to extend indexer type beyond number | string | bingint.
novemberborn commentedon Nov 14, 2018
Sorry, I was suggesting that there are other types that work in JavaScript, but for sensible reasons are not supported in TypeScript.
NN--- commentedon Nov 14, 2018
BigInt is allowed to be index in ES, so there is no question here.
Btw, I think there was no issue about type with toString property as indexer.
calebsander commentedon Nov 14, 2018
We considered this in #25886 and decided against adding
bigintas an index type. See my comment: #25886 (comment). As Mark Wubben (@novemberborn) says, any value can be used as a key in JS because any value can be converted to a string. Really,stringandsymbolare the only true index types.numberis included becauseArrays andTypedArrays are meant to be indexed with numeric indices. Sincenumbercan store a 53-bit unsigned integer, it is sufficient to index anything stored in almost all modern address spaces (e.g. the 48-bit x86-64 address space). There is really no reason to use abigintas a numeric index unless you have a sparse array (in which case an object should be used instead). You can always just convert thebigintto anumberorstring, which would be a valid index type.NN--- commentedon Nov 15, 2018
Thanks.
Good point.
schmidsi commentedon Dec 20, 2018
It is probably a bit off-topic but I searched some time for a possibility to use BigInt today and came across this project: https://github.com/GoogleChromeLabs/jsbi
It has the same API and there is a babel-transform plugin to remove it eventually.
calebsander commentedon Dec 20, 2018
Integrating JSBI was already discussed in #28756
sniten commentedon Sep 4, 2020
Merge pull requested