Member-only story

Do Not Use TypeScript Enum?

What is Enum’s fault?

Xiuer Old

--

Introduction: The Enum Controversy 🎭

TypeScript enums have been a subject of debate since their inception. Many developers, including seasoned professionals, have voiced concerns about their usage, leading to a “DO NOT USE TypeScript Enum” movement. But are enums really that problematic? Let’s explore the recent developments and alternatives that have sparked this conversation.

The Erasable Syntax Revelation 🧐

With the release of TypeScript 5.8, a new configuration option called --erasableSyntaxOnly was introduced. This option only allows "erasable syntax" in TypeScript files. But what exactly is erasable syntax, and why does it matter?

Erasable vs. Non-Erasable Syntax

  • Erasable Syntax: This includes TypeScript-specific constructs that don’t generate additional runtime code, such as type, interface, and type annotations like let n: number.
  • Non-Erasable Syntax: These are constructs that generate additional JavaScript code at runtime, including enum, namespace (with runtime code), and class parameter properties.

The Node.js Connection 🔗

--

--

Xiuer Old
Xiuer Old

Written by Xiuer Old

🔥Little brother teaches front-end and AI online🌈

Responses (1)