5 ms·
Compiling TypeScript is just stripping out types
by warpech 1mo ago
Compiling TypeScript is just stripping out types
- Stratoscope 1mo agoThat is true only if you confine your TS code to be pure JS plus type annotations. There are TS constructs that require compilation into equivalent JS, such as enums and namespaces.
- hermanradtke 1mo agoAre there any other? Namespaces are heavily discouraged. Some teams still use enigma but most people in the know us unions of strings. I know the TS team regrets enums and I have not seen any plans for adding additional keywords that break type stripping.
- Stratoscope 1mo agoThe other one I read about was parameter properties in constructors. And thank you for the correction! It was quite a few years ago that I used TypeScript, and I think the landscape was different then. Making things even fuzzier in my mind, this was an Angular project, and it was hard to keep track of what was Angular and what was TypeScript. It sounds like people these days actively avoid the parts of TypeScript that require translation into different JS code.
- igl 1mo agoabsolutely nobody used those outside of a language demo. We also got --erasableSyntaxOnly last year to remove them from the language. The TS team considers them “mistakes“ for a really long time now
- Stratoscope 1mo agoThanks for the correction! It was a long time ago that I used TypeScript, and making things worse (for my memory) it was an Angular project. So today, most people and teams avoid the parts of TS that require compilation into different JS, and --erasableSyntaxOnly lets you enforce this, right?
- igl 1mo agoExactly. There is plenty of code change however introduced by bundlers who do “bundling“, polyfill injection, minification, this kind of stuff.
- p_l 1mo ago"fortunately" we can load swc compiled to wasm to deal with that.
- gleenn 1mo agoIsn't that like saying the Internet is only written in English if you strip out all the non-English. All C-style languages are fully cross compatible if you only use semicolons. I can't write Typescript into a browser without cross-compilation and "stripping out types" involves parsing and manipulating code. That's a different language.
- jitl 1mo agoNo. 98% of typescript specific syntax can be replaced with U+0020 SPACE to yield runnable JavaScript. The runtime semantics of the two languages are congruent. the same cannot be said for any other language im aware of.
- gleenn 1mo agoI'm sorry but if you need a transpiler to get it to run in a browser, character replacability and runtime semantics don't matter. Typescript doesn't run in the browser, it's obviously a different language.