Given existing enum HttpMethod, when you want to replace existing value "POST" with e.g. "post", you change enum's value and

With or without enums in TypeScript

submited by
Style Pass
2024-04-25 23:00:03

Given existing enum HttpMethod, when you want to replace existing value "POST" with e.g. "post", you change enum's value and you're done!

If you're not familiar with Opaque types, it's a way to declare types of the same structure, which are not assignable to each other.

The perfect example can be 2 currencies (e.g. USD and EUR). You cannot simply put dollars into euro account without taking into account currency exchange rate:

Please check the playground to test it for TypeScript 4.9.5 - https://www.typescriptlang.org/play?preserveConstEnums=true&ts=4.9.5#code/KYOwrgtgBA8mAuAHBUDeAoKWoFEBOeA9nlALxQCMANJtgOoCGeIAliAOZlQBMN2UAGUKdyAZhoBfdOjbxgeAGYMAxsFiJ4LQiADOaWlkIJk8APwAuWMYQBudFPTLtO+FEIatuyzA-OuqCTt3TWcAOiMkFHI4SPhQ-CI8IN9dcOtXaPTQxmY2dmSQ1IiTLhiTUKF86QB6ard3HSooACMUFxYAGw6oHQYFYHRgzx002K5RO3RaqABCOdgYAAUAZVn5sF0+tUBeDcBQPcGUkeKoqAAWAuHRkvIAVhsgA 🏝️

Beginning with TypeScript 5.0, this issue has been resolved and addressed within the release, eliminating it as a concern going forward. For more details, please read TypeScript 5.0 Release | Enum Overhaul.

Leave a Comment