HeadlinesBriefing favicon HeadlinesBriefing.com

TypeScript --erasableSyntaxOnly Explained: Why & When to Use

DEV Community •
×

TypeScript 5.8 introduced a new compiler flag, --erasableSyntaxOnly, designed to enforce future-proof coding patterns. This flag causes compilation errors for TypeScript-specific syntax that requires runtime code generation, such as enums, namespaces with runtime behavior, parameter properties, and old-style type assertions. The introduction of this flag aligns with two major industry movements: Node.js's native TypeScript support via --experimental-strip-types, which only strips types, and the TC39 'Types as Comments' proposal, which aims to let browsers ignore type syntax.

These initiatives require TypeScript syntax to have no runtime impact. While the community often jokes about deprecating enums, the TypeScript team has explicitly declined to do so to maintain backward compatibility. However, they recommend modern alternatives like 'as const' objects for enums and ES modules for namespaces.

The --erasableSyntaxOnly flag is an opt-in tool that encourages developers to adopt these patterns gradually, ensuring code remains compatible with future JavaScript standards and native execution environments without breaking existing codebases.