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.

const enum with positive number literal preceeded with + causes errors with typed enum access but not with declaration

See original GitHub issue

Bug Report

🔎 Search Terms

  • “const enum”
  • literals
  • 2535
  • 2474

This issue may also be somewhat relevant to https://github.com/microsoft/TypeScript/issues/37783

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about const enums

⏯ Playground Link

Playground link with relevant code

💻 Code

const enum Test {
    a = -1,
    b = 0,
    c = 1,
    d = +2, // no error - expected
}

type test = Test;

type a = Test.a; // error 2535 - unexpected
const a = Test.a;
type b = Test.b; // error 2535 - unexpected
const b = Test.b;
type c = Test.c; // error 2535 - unexpected
const c = Test.c;
type d = Test.d; // error 2535 - unexpected
const d = Test.d;

const enum Test2 {
    a = -1,
    b = 0,
    c = 1,
    d = {}, // error 2474 - expected
}

type test2 = Test2;

type a2 = Test2.a; // error 2535 - expected
const a2 = Test2.a;
type b2 = Test2.b; // error 2535 - expected
const b2 = Test2.b;
type c2 = Test2.c; // error 2535 - expected
const c2 = Test2.c;
type d2 = Test2.d; // error 2535 - expected
const d2 = Test2.d;

const enum Test3 {
    a = -1,
    b = 0,
    c = 1,
    d = 2, // no error - expected
}

type test3 = Test3;

type a3 = Test3.a; // no error - expected
const a3 = Test3.a;
type b3 = Test3.b; // no error - expected
const b3 = Test3.b;
type c3 = Test3.c; // no error - expected
const c3 = Test3.c;
type d3 = Test3.d; // no error - expected
const d3 = Test3.d;

🙁 Actual behavior

Error 2535 occurs for type based access of const enums with a literal number preceeded by +, however, error 2474 does not occur when declaring such a const enum. The JavaScript output works as intended though it does strip out the preceeding + from it.

🙂 Expected behavior

I expect type based access of the const enum to not result in error 2535 (Enum type 'Test' has members with initializers that are not literals.).

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ahejlsbergcommented, Nov 4, 2022

Huh, in that case it seems weird…

Agreed, the fact that we had obtuse error messages in this feature area definitely didn’t help. With #50528 I think we finally have an implementation that is explainable without suprises and inconsistencies.

0reactions
fatcerberuscommented, Nov 4, 2022

Huh, in that case it seems weird that Test1 doesn’t produce a TS2474 error like Test2 does. It’s inconsistent that TS considers +2 a literal within the enum declaration (const enum can only contain literal values), but then go on to later claim that the enum contains non-literal values when you try to use its element types.

Water under the bridge now, of course, but I don’t know that I’d accept this as “by design” if it hadn’t already been fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Enums - TypeScript
TypeScript provides both numeric and string-based enums. ... enum is simple: just access any member as a property off of the enum itself,...
Read more >
const enum in Typescript - Stack Overflow
When I try something similar I'm getting the following error: error TS2476: A const enum member can only be accessed using a string...
Read more >
Enumeration declaration - cppreference.com
An enumeration is a distinct type whose value is restricted to a range of values (see below for details), which may include several ......
Read more >
P4~16~ Language Specification
The error type ... The declaration contains several type declarations, constants, and ... Integer literals are positive, arbitrary-precision integers.
Read more >
4.1. Specification of MiniZinc
All global variable names. All function and predicate names, both built-in and user-defined. All enumerated type names and enum case names. All annotation...
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