Categories
TypeScript Answers

How to cast int to enum strings in TypeScript?

Spread the love

Sometimes, we want to cast int to enum strings in TypeScript.

In this article, we’ll look at how to cast int to enum strings in TypeScript.

How to cast int to enum strings in TypeScript?

To cast int to enum strings in TypeScript, we can use square brackets.

For instance, we write

export enum Type {
  Info,
  Warning,
  Error,
  Fatal,
}

to define the Type enum.

Then we use Type[Type.Info] to return type 'Info'.

Conclusion

To cast int to enum strings in TypeScript, we can use square brackets.

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 *