About the only positive that is qualitatively different is the simplicity of comparing the output to the input. But for someone building a large typescript codebase, and who uses sourcemaps, it's not really a big issue. There are many many languages that compile-to-JS, and I feel that insisting on 'purity' for purity's sake isn't really a good justification. That, TS as a super-set of JS instead of as a isomorphic mapping between constructs is a perfectly viable way to innovate in the language space.
But this doesn't justify removing every codegen feature. Namespaces and enums won't make your code less reasonable, and moreover they're just syntax sugar, they don't change actual JS semantics.
Besides, there is an straightforward way to remove enums from a program just like removing type annotations: Inline them as static fields of an object.
There is a simple syntactic transformation.
e.g. change 'enum' to 'const', add a '=' before the '{'
and use ':' instead of '='
const HttpMethod = {
Get: 'GET',
Post: 'POST'
};
// now this no longer breaks
const method = HttpMethod.Post;
Namespaces can be translated in almost the exactly same way. const HttpMethod = {
Get: 'GET',
Post: 'POST',
} as const;
type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
Maybe wrap the `{…} as const` in Object.freeze(…) for good measure.It’d be really nice if they’d improve the ergonomics on this in some way (`type Values<T> = T[keyof T]` would reduce it to `type HttpMethod = Values<typeof HttpMethod>`, which is a start but not enough), to make it a genuine and suitable alternative to enum (minus the other frippery that’s generated) and const enum (because it’s pure JavaScript, not an extension).
One cannot use Typescript without understanding Javascript, since every single Javascript library is not written in Typescript.
No one should need or worry about what an abstraction compiles to unless they’re specifically working on that abstraction or there’s a language bug.
> One should also understand the machine code that a traditional language compiles to and how the CPU executes it. Ultimately, your language compiles to machine code. /s
If libraries you use are all written in machine code, then sure, you should have an understanding of machine code. Your comparison clearly doesn't work here.
> No one should need or worry about what an abstraction compiles to unless they’re specifically working on that abstraction or there’s a language bug.
When an abstraction is that leaky, it's barely an abstraction. Typescript does force you to choose a Javascript version as a compilation target. Obviously you are forced to know what Javascript version supports what feature because Typescript isn't going to polyfill every missing feature depending on your Ecmascript target.
The problem with Typescript is that too many Typescript developers don't understand Javascript itself, which is an completely different issue. That and the obsession for some to reproduce JEE everywhere including in the browser...
I really dislike this tendency of certain Javascript developers to qualify combining OOP with Javascript as "not understanding the language".
But before that there were countless competing models for creating objects or object factories.
Obviously, JavaScript is an object oriented language, too. You cannot escape that fact if you are determined to make the browser paint anything.
Classes effectively solved the "How?"
To pretend you don't need object orientation in JavaScript is really trying hard to make JavaScript into an entirely different language.
Java and C# had object factories even though in those languages classes could not be avoided. People wanted classes because they could not figure how to program without them.
> To pretend…
Don’t use this or new in your code and suddenly a tremendous amount of your code is exposed as unnecessary superfluous vanity. That isn’t making the language into something else.
Object factories were competing models (plural) for creating any object. A total replacement for classes and the like, not an augmentation.
Here's one such model:
function createCar(spec) {
const {speed} = spec;
let position = 0;
return Object.freeze({
move() {
position += speed;
},
get position() {
return position;
}
});
}
And so you'd find this or any other model or multiple competing models in the very same code base.It sucked.
> Don’t use this or new in your code
You are going to be mutating the internal state of objects. Using this and new or not.
Proxies and classes were definitely salvaged from ES4/ActionScript/Jscript.net.
We wouldn't be needing Typescript, had ES4 been adopted (ironically Microsoft was against it, because Silverlight...).
Someone definitely needs to write a book about the whole saga.
Honestly most of these features are really useful, and as someone who never wants to work in raw JavaScript, I wish they were just added to JavaScript instead. Why shouldn’t JavaScript have enums, namespaces, private modifiers which aren’t #, and decorators? JS already gets new features which break backward-compatibility, mine as well add features which fix compatibility with TypeScript.