Categories
TypeScript Answers

How to fix an enum type not defined at runtime with TypeScript?

Spread the love

Sometimes, we want to fix an enum type not defined at runtime with TypeScript.

In this article, we’ll look at how to fix an enum type not defined at runtime with TypeScript.

How to fix an enum type not defined at runtime with TypeScript?

To fix an enum type not defined at runtime with TypeScript, we can use the const or export keyword before the enum.

For instance, we write

const enum MyEnum {
  One = "one",
  Two = "two",
}

to create the MyEnum enum with the const keyword before it to keep it in the compiled JavaScript code as an object.

We can also write

export enum MyEnum {
  One = "one",
  Two = "two",
}

if we want to export the module and use it in another module.

Conclusion

To fix an enum type not defined at runtime with TypeScript, we can use the const or export keyword before the enum.

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.