Skip to content

BigInt Literal Support in Earlier ECMAScript VersionsΒ #62253

@Vessel9817

Description

@Vessel9817

πŸ” Search Terms

BigInt literals ES2020

βœ… Viability Checklist

⭐ Suggestion

In ES versions >= ES2016 and < ES2019, the transpiler should convert BigInt literals to their equivalent object instantiation. This proposal implements backwards compatibility for BigInt literals, seemingly without any technical or logistical challenges.

Playing devil's advocate, this proposal could technically be considered syntactical sugar, as it brings no further functionality to TypeScript. However, it is conversely a JavaScript feature whose support could be extended.

πŸ“ƒ Motivating Example

The following sample works fine in ES2016:

BigInt('-5') ** BigInt('3') // BigInt('-125')

This isn't very readable, given that JavaScript implements a shorthand that should be easily implementable:

(-5n) ** 3n // -125n

πŸ’» Use Cases

  1. What do you want to use this for?

This proposal would resolve the following TS error:

BigInt literals are not available when targeting lower than ES2020. ts(2737)

Note that this compatibility resolution should not affect the following error in any capacity:

Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. ts(2791)

  1. What shortcomings exist with current approaches?

Syntactical bloat, and potential tiny performance losses from instantiating separate BigInt objects over using literals.

  1. What workarounds are you using in the meantime?

Instead of BigInt literals (e.g, 5n), use the BigInt constructor (e.g, BigInt('5')).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions