Skip to content

Cannot use const string in enum when it's type is specified explicitly #57199

@undsoft

Description

@undsoft

🔎 Search Terms

enum string, enum const

🕗 Version & Regression Information

  • This is the behavior in every 5+ version available in Playground.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZgQwJYBsIC4YHIAqBTADyixgF5t8isBuAKFElgHcQAnAawjIsOLtrxgArgFsYARSF5oScDgCeABzwwA3rRgxKscolQQANBq28ATNxYdDtAL5A

💻 Code

const fails: 'Text' = 'Text';
const works = 'Text';

enum QuestionType {
  Text = fails,
  Text2 = works,
}

🙁 Actual behavior

When fails is used in enum, there is an error:

Type 'string' is not assignable to type 'number' as required for computed enum member values.(18033)

🙂 Expected behavior

I can use fails, because to me it is equivalent to works.

Additional information about the issue

Related to #29718 ?

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions