Reservations about TypeScript
daviesgeek.com
daviesgeek.com
I'll point out that C/C++/Haskell compiles down to Assembly, which doesn't have types or type checking. GCC/LLVM/GHC is great at checking at compile time, but at the end of the day, it's all just bits being modified...
Obviously you can find ways to break out of the type systems (void* cast?) but that's on you, and even if Typescript has more escape holes, the existence of them isn't an immediate fail
That being said, TS does suffer from something unique, in its JS interop. If my C program interfaces with a C library, I have great certainty that all of these were compiled at some point, and the types are sound (as much as C allows, at least).
In TS, if you're lucky a library you consume was written in TS, so you've got high certainty that the typings it comes with are really good. Less lucky: the developer wrote it in JS, but provides typings. Less lucky: a random person on Github reverse-engineered the typings and provided them on DefinitelyTyped. Less lucky: There are no typings available. You'll run into npm libraries in all of these categories.
In my experience, even if you don't have types for a third-party library, you can get away with adding types for the interfaces you care about.
You can also accidentally have these issues if you are using incompatible pieces of software together. I was helping some colleagues with exactly this problem this week due to some changes in our toolchain.
EDIT: I wasn't clear. This was in a C++ codebase, but the same issue would have happened in C or any other compiles-to-assembly language.
Sometimes a type system can be proven to have no holes. (Of course, not counting the FFI, if any is provided.)
And usually there's something in the unsafe category that lets you break some rules, if only to give a pressure release as in the interop reply above.
I don't deny the existence of such a thing, just that there aren't many practical examples
Second point: "It adds a feature that's different from a similar language."
These are not persuasive arguments. I'm not trying to be unkind, but the first point is totally unsupported and the second doesn't elaborate on what's bad about classes besides that another language also has classes.
> TypeScript brings some great features, but, in my opinion, it comes with some significant pitfalls that need to be addressed and realized before jumping in with both feet.
While listing none of the pitfalls unless the significant pitfall is classes mentioned a few paragraphs above.
This reads like it had much grander aspirations and ended up being published 30% through.
"TypeScript still compiles down to JavaScript. It has no way of actually checking types." That's my point. Maybe I should make it more obvious in the article :) I'm not complaining about the fact that TS has classes or types, it's that TS compiles down to a language that has neither.
The implication that because TypeScript compiles to JS which has no types and is therefore wrong is pure drivel.
Compilers which do add type information into the output are usually paired with a runtime that uses it for things like late binding or dynamic dispatch, but JavaScript doesn't support anything like that.
Could you give a concrete example of a scenario in which the flexibility of the JavaScript type system would break the TypeScript type system?
My concern is the facade/promise of type security.
See https://www.typescriptlang.org/docs/handbook/type-compatibil... for a less cynical presentation of these design decisions.
Also, FWIW, in my experience, I don't think I've ever actually run into a problem with this in real code.
What languages compile down to a language that has either? Most languages compile down to either machine code, or virtual machine code.
It looks like the author started out as saying one thing (how Typescript is fundamentally different from JS) and ended up with another (how they are fundamentally the same) :-)
Which reminds me. Could you please explain to me what are the proper classes? I guess I have spent too much time with JavaScript to start thinking about classes just as about collocations of methods and properties spewed out by constructors. What is so improper about JavaScript's prototype-based classes that is different in real classes?
How you implement inheritance under the hood and how you interpret the `class` keyword is another false distinction the author makes.
I mean, his second "reservation" is that TypeScript "fundamentally" changes JavaScript. How does he demonstrate this? By complaining that classes, which are a JavaScript feature, work differently than in other OO languages. What the hell does that even have to do with TypeScript?
Opinions are like buttholes, everone has one.
I think I was pretty clear that it was all just my opinion :) If you read the end of the post I said that maybe I'm just worrying over something that's a non-issue, so by all means, steer me in the right direction rather than critiquing the fact that I'm expressing my opinions ;)
The fact that TS isn't sound has nothing to do with the fact it compiles to JS. If you wanted a sound type system, you should have checked out flow.
Good enough that I usually debug the Javascript output. Typescript has the cleanest transpilation I've ever seen so I don't have any problems with using it. And if Typescript ever falls out of vogue I'm confident just compiling down to JS one last time and being done with it. Really the language is so easy to get rid of its harmless.
Some examples:
https://github.com/BuckleScript/bucklescript-addons/tree/mas...
There are other languages with strong, powerful type systems that compile to efficient JavaScript, such as (in order of power) Elm, OCaml, PureScript and Idris. Those four are just arbitrary examples however, there are quite a few more.
It's a work of art, everything is so practical and (relatively) easy to understand. Even better, it's written in TypeScript, so you can open it up in an editor with TS support and follow/jump to any reference with confidence. I've only learned about compilers vaguely through osmosis, but I was able to extend its JSX syntax processing (admittedly that part does seem a bit bolted-on) to output JSON for my wonky framework side-project in a few hours: https://github.com/guscost/protozoa-tsx