-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot use const string in enum when it's type is specified explicitly #57199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Enum members may be initialized by constant expressions that compute string or numeric values. Constant expressions may reference |
I think this is probably the thing that sounds surprising, but presumably it's to ward off problems like this? // Correct emit is ... ?
const fails: number | 'Text' = 'Text'; |
That's one reason, but another is to avoid circularities. Imagine that a type annotation depends on the enum type for which we are in the process of evaluating constant expressions. |
Isn't @RyanCavanaugh argument already somewhat trumped by allowing this in other similar situations like this one? // input
const one: number | "Text" = "Text";
const two = one;
// dts output
declare const one: number | "Text";
declare const two: "Text"; I also found a confusing error to be reported in a related situation: const a = E.two; // A member initializer in a enum declaration cannot reference members declared after it, including members defined in other enums.(2651)
const b = `pre${a}`;
const enum E {
one = "foo",
two = `${E.one}bar`,
} The location of the error seems to be wrong. It mentions a member initializer but the error node is completely different. On top of that, the referenced enum member doesn't depend on any member declared after it. This problem goes away if we remove |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
enum string, enum const
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZgQwJYBsIC4YHIAqBTADyixgF5t8isBuAKFElgHcQAnAawjIsOLtrxgArgFsYARSF5oScDgCeABzwwA3rRgxKscolQQANBq28ATNxYdDtAL5A
π» Code
π Actual behavior
When
fails
is used in enum, there is an error:π Expected behavior
I can use
fails
, because to me it is equivalent toworks
.Additional information about the issue
Related to #29718 ?
The text was updated successfully, but these errors were encountered: