Categories
TypeScript Answers

How to fix the “A const enum member can only be accessed using a string literal.” error with TypeScript?

Spread the love

Sometimes, we want to fix the "A const enum member can only be accessed using a string literal." error with TypeScript.

In this article, we’ll look at how to fix the "A const enum member can only be accessed using a string literal." error with TypeScript.

How to fix the "A const enum member can only be accessed using a string literal." error with TypeScript?

To fix the "A const enum member can only be accessed using a string literal." error with TypeScript, we should remove const before the enum definition.

For instance, instead of writing

const enum Snack {
  Apple = 0,
  Banana = 1,
  Orange = 2,
  Other = 3,
}

we write

enum Snack {
  Apple = 0,
  Banana = 1,
  Orange = 2,
  Other = 3,
}

Conclusion

To fix the "A const enum member can only be accessed using a string literal." error with TypeScript, we should remove const before the enum definition.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.