-
Notifications
You must be signed in to change notification settings - Fork 13k
Description
π Search Terms
BigInt literals ES2020
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
- 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)
- What shortcomings exist with current approaches?
Syntactical bloat, and potential tiny performance losses from instantiating separate BigInt objects over using literals.
- What workarounds are you using in the meantime?
Instead of BigInt literals (e.g, 5n
), use the BigInt constructor (e.g, BigInt('5')
).