Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change the value of enum in TypeScript

Have a look at the following code:

// typescript enum example
enum foo { ONE = 1, TWO = 2, THREE = 3 }

Is it possible to change the value of ONE to 0 in runtime? If yes, how?

like image 975
Omar Huseynov Avatar asked Oct 16 '25 10:10

Omar Huseynov


1 Answers

Is it possible to change the value of ONE to 0 in runtime? If yes, how?

You can always use a type assertion:

// typescript enum example
enum foo { ONE = 1, TWO = 2, THREE = 3 }

(foo as any).ONE = 0;

More : https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html But I don't recommend it.

Why not just write the following in the first place?:

enum foo { ONE = 0, TWO = 2, THREE = 3 }
like image 75
basarat Avatar answered Oct 19 '25 00:10

basarat