const enum with positive number literal preceeded with + causes errors with typed enum access but not with declaration
See original GitHub issueBug 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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >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
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.
Huh, in that case it seems weird that
Test1
doesn’t produce a TS2474 error likeTest2
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.