You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TS 2535 when using const enum values derived from other const enum values #35875
// A *self-contained* demonstration of the problem follows...// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.constenumFoo{Value=0x01}constenumBar{Value=0x02}constenumBaz{Value=Foo.Value|Bar.Value}typeTest=Baz.Valueconstfoo: Foo.Value=Foo.Valueconstbar: Bar.Value=Bar.Valueconstbaz: Baz.Value=Baz.Value
Expected behavior: It to type-check without errors
Actual behavior: "Enum type 'Baz' has members with initializers that are not literals. (2535)" on both the type alias and the baz type, referencing Baz.Value in the type position.
My immediate use case is that of using a discriminated union based on part of a bit mask. Something roughly like this, but on a bit larger of a scale:
exportconstenumMask{
Type =0x00FF,
IsStatic =1<<4,}exportconstenumType{
One =0x00,
Two =0x01,
Three =0x02,}exportconstenumMaybeStatic{
One =Type.One,
Two =Type.Two,
Three =Type.Three,
StaticOne =Type.One|Mask.IsStatic,
StaticTwo =Type.Two|Mask.IsStatic,
StaticThree =Type.Three|Mask.IsStatic,}exporttypeNode=NodeOne|NodeTwo|NodeThree;exportinterfaceNodeOne{
type: MaybeStatic.One|MaybeStatic.StaticOne;
value: string;}exportinterfaceNodeTwo{
type: MaybeStatic.Two|MaybeStatic.StaticTwo;
value: number;}exportinterfaceNodeThree{
type: MaybeStatic.Three|MaybeStatic.StaticThree;
value: symbol;}
My workaround is specifying these directly, but this is very much so not ideal. (I'm trying to take advantage of TS's constant evaluation to avoid duplicating work with the compile-time constants.)
constenumA{a=1}constenumB{b=A.a}consta=A.a;// typeof 'a' is 'A.a'constb=B.b;// typeof 'b' is 'B' rather than 'B.b'
that's because typescript requires only literal members of a const enum can be used as a type. but typescript regards B.b's initializer A.a as a computing expression rather than a literal, even though this computing expression can be computed in compile time. note that is literal is a stricter requirement than can be computed in compile time.
In the TypeScript handbook about enums, it says that a reference to previously defined constant enum member (which can originate from a different enum) is also consider a constant enum expression, which seems to contradict to this issue.
Maybe the (which can originate from a different enum) part from the handbook is not implemented?
Activity
dead-claudia commentedon Dec 27, 2019
Accidentally filed this - please ignore.Updated with intended text.[-]2535[/-][+]TS 2535 when using const enum values derived from other const enum values[/+]dead-claudia commentedon Dec 27, 2019
My immediate use case is that of using a discriminated union based on part of a bit mask. Something roughly like this, but on a bit larger of a scale:
My workaround is specifying these directly, but this is very much so not ideal. (I'm trying to take advantage of TS's constant evaluation to avoid duplicating work with the compile-time constants.)
zimtsui commentedon Dec 23, 2020
same problem, in my version 4.0.2
that's because typescript requires only literal members of a const enum can be used as a type. but typescript regards
B.b's initializerA.aas a computing expression rather than a literal, even though this computing expression can be computed in compile time. note that is literal is a stricter requirement than can be computed in compile time.actually I don't think it's a good convention to use a member of a const enum, like
A.a/B.b, as a type, it has ambiguous semantics.so there is a workaround:
peter50216 commentedon Jan 11, 2022
In the TypeScript handbook about enums, it says that
a reference to previously defined constant enum member (which can originate from a different enum)is also consider a constant enum expression, which seems to contradict to this issue.Maybe the
(which can originate from a different enum)part from the handbook is not implemented?Walkalone13 commentedon Oct 19, 2023
got this in such case:
resolved with using literals without any calculations
dead-claudia commentedon Oct 19, 2023
Walkalone13 Please file a new issue. This one's been closed for over a year.