question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support bigint literals in const enums

See original GitHub issue

Search 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:open
  • Created 3 years ago
  • Reactions:19
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
connor4312commented, Apr 3, 2020

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 like

const enum IPv4Masks {
  MulticastMask = 0x00_ff_ff_ff,
  // ...
}

But IPv6 addresses are 64-bit integers. So, because const enums don’t support bigints, I’m unable to similarly do

const enum IPv6Masks {
  MulticastMask= 0x00ff_ffff_ffff_ffff_ffff_ffff_ffff_ffffn, 
  // ...
}

You could also easily imagine large bitsets with >52 values.

const enum Permissions {
  Read = 1n << 0n,
  Write = 1n << 1n,
  // ...
  BeFancy = 1n << 58n,
}

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.

6reactions
yseymourcommented, Aug 12, 2020

You could also easily imagine large bitsets with >52 values.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found