Skip to content

Support for TC39 "BigInt: Arbitrary precision integers in JavaScript" proposal #15096

Description

The ECMAScript Technical Committee 39 has put forward a concrete proposal for adding integers to JavaScript:

https://tc39.github.io/proposal-bigint/

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.

Activity

  1. aluanhaddad commented on Apr 10, 2017

    @aluanhaddad
    Contributor

    🚲:house:
    Why didn't they choose Z for the literal suffix?

  2. 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
  3. tarcieri commented on Jul 11, 2017

    @tarcieri
    Author

    Looks like this may be landing in Babel soon:

    babel/proposals#2

  4. tarcieri commented on Jul 26, 2017

    @tarcieri
    Author

    Initial (WIP) babel PR for this: babel/babel#6015

  5. Cixelyn commented on Aug 1, 2017

    @Cixelyn

    Looks like the BigInt proposal has also hit stage 3 (tc39/proposal-bigint@1b4c7e6). Would be awesome to see TypeScript support.

  6. raix commented on Jan 15, 2018

    @raix

    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.

  7. styfle commented on Mar 7, 2018

    @styfle
    Contributor

    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?

  8. Bnaya commented on Mar 7, 2018

    @Bnaya

    BigInt is implemented in V8, means we will have it in chrome and V8 soon
    refs:
    https://twitter.com/bmeurer/status/969656997879713792

  9. samrg472 commented on Jun 7, 2018

    @samrg472

    Node 10.4.0 ships with V8 6.7, a flag is no longer needed to be able to use big integers.

  10. Kingwl commented on Jun 8, 2018

    @Kingwl
    Contributor

    seems have a plan to support it on 3.0

  11. styfle commented on Jun 8, 2018

    @styfle
    Contributor

    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

  12. 19 remaining items

  13. gatsbyz commented on Sep 20, 2018

    @gatsbyz

    Thanks for this!

  14. NN--- commented on Nov 14, 2018

    @NN---

    What about indexer type ?
    This code is valid JS:

    [1,2,3][1n]

    It means that indexer type now has to be allowed as: string | number | bigint

  15. novemberborn commented on Nov 14, 2018

    @novemberborn

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

  16. NN--- commented on Nov 14, 2018

    @NN---

    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.

    type Indexer = number | string | bigint | ({ toString(): string|number }) 
  17. novemberborn commented on Nov 14, 2018

    @novemberborn

    Sorry, I was suggesting that there are other types that work in JavaScript, but for sensible reasons are not supported in TypeScript.

  18. NN--- commented on Nov 14, 2018

    @NN---

    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.

  19. calebsander commented on Nov 14, 2018

    @calebsander
    Contributor

    What about indexer type ?
    This code is valid JS:

    [1,2,3][1n]

    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.

  20. NN--- commented on Nov 15, 2018

    @NN---

    Thanks.
    Good point.

  21. schmidsi commented on Dec 20, 2018

    @schmidsi

    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.

  22. calebsander commented on Dec 20, 2018

    @calebsander
    Contributor

    Integrating JSBI was already discussed in #28756

  23. sniten commented on Sep 4, 2020

    @sniten

    Merge pull requested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueES NextNew featurers for ECMAScript (a.k.a. ESNext)SuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions