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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *