TypeScript Gets More JavaScript Compatible with --erasableSyntaxOnly Flag

TypeScript just got a whole lot more interesting. So, what's the big deal about this new flag, --erasableSyntaxOnly? It's all about making TypeScript more compatible with JavaScript - and it's related to some major efforts in the Node.js and TC39 communities. You see, when you enable this flag in your tsconfig.json, you'll start getting compile errors for certain TypeScript syntax that's not "erasable". It's not allowed: enum declarations, namespaces with runtime code, and a few other things. Why? Because they can't be simply removed without affecting how your JavaScript code runs. For instance, take a look at this function: it's erasable. Function greet is simple. You can strip away the type, and it'll still work just fine. But enums, on the other hand, are a different story - if you remove one, you'll get a runtime error, and that's not what you want. So, what's driving this change? Well, Node.js 23.6 has a new flag, --experimental-strip-types, which lets you run TypeScript files directly, but it only strips types, and doesn't support enums or namespaces. And then there's the TC39 proposal, "Types as Comments", which wants to make browsers ignore TypeScript type syntax, like it's just a comment. If this proposal succeeds, you'll be able to run TypeScript without a build step - but for that to work, types need to be "erasable". So, what does this mean for your code? You don't need to enable --erasableSyntaxOnly just yet, but for new code, it's a good idea to use alternatives to non-erasable syntax, like using an object with the `as const` keyword instead of an enum. The TypeScript team is not deprecating enums, but they are encouraging you to use alternatives in new code - it's all about innovation, strategy, and creativity in coding. Check out the source for more info: https://lnkd.in/gryaNcAs #TypeScript #Innovation #CodingStrategy

To view or add a comment, sign in

Explore content categories