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.