Support bigint literals in const enums
See original GitHub issueSearch Terms
- “const enum member initializers can only contain literal values”
- “bigint enum”
- “ts(2474)”
Suggestion
A bigint literal is a literal, but it does not seem usable in const enums.
const enum Foo {
Bar = 123n
}
Currently produces, on 3.9.0-beta:
const enum member initializers can only contain literal values and other computed enum values. ts(2474)
Use Cases
Bigint compile-time constants 🙂
Examples
const enum Foo {
Bar = 123n
}
console.log(Foo.Bar);
Should compile to
console.log(123n);
And log 123n
when run.
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:19
- Comments:16 (4 by maintainers)
Top Results From Across the Web
how to create an enum-like object with bigint initializers in ...
I simplified my use-case for the question, but the idea is the same. Basically the enum value is used in a loop that...
Read more >Documentation - TypeScript 3.2
In TypeScript 3.2, object literals now allow generic spread expressions which ... BigInt support in TypeScript introduces a new primitive type called the ......
Read more >13 Alternatives to enums in TypeScript
An enum maps member names to member values. If we don't need or want the indirection, we can use a union of so-called...
Read more >Integer literal - cppreference.com
If the value of the integer literal is too big to fit in any of the types allowed by suffix/base combination and the...
Read more >zod - npm
Currently there is no support for Date literals in Zod. ... Alternatively, use as const to define your enum values as a tuple...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The use case would be the similar to using normal
numbers
in enums–just for scenarios where we’re dealing with integers that don’t fit into float64’s. The place I ran into this is dealing with IPv6 addresses. For IPv4 addresses, I can have an enum of well-known masks likeBut IPv6 addresses are 64-bit integers. So, because const enums don’t support bigints, I’m unable to similarly do
You could also easily imagine large bitsets with >52 values.
I don’t think there should be an error message at all here, since bigint literals are just another type of literal; restricting their use in const enums seems arbitrary.
Actually, >31 values, since bitwise operators in JS convert Number operands to signed 32-bit which makes the 32nd bit effectively unusable. Being able to use BigInts would be very helpful for interop with native code.