Is TypeScript worth it?
h
MocaCDeveloper

Simple question, yet seems to be very debatable from person to person.

Question

Is TypeScript worth learning. It seems to have a very clean syntax and seems to be written smoothly. It has some strong ties with JavaScript(duh).

But, I have heard from many developers that it is slow and "too much like JavaScript", and in that case many developers lean more towards JavaScript due to the fact there belief is that TypeScript is too much like JavaScript.

But, I need a straightforward answer. Is TypeScript worth learning?

I am on a learning spree. I am currently learning Rust, I am getting a little hands on with C++(I might create a game engine using C++), and I want to get into some other languages like JavaScript or TypeScript for server-side development(or whatever TypeScript is used for).

I've seen allot of interesting projects in TypeScript and I feel like it would fit my needs in some projects I want to write where in most cases I don't want to use Python(mainly because Python gets a bit boring over time, and languages like Rust, C, C++ etc would take far too long).

So, to me, TypeScript would fit in my category of a language to learn. I don't want to dive into JavaScript due to its strong bonds with Web Development.

But, I really don't know anything about TS or JS. Shoot, they could both be the absolute same thing..just different languages.

But what do you guys think? Is TS worth learning, or should I dive right into JS?

You are viewing a single comment. View All
xxpertHacker

TLDR: Imho, TS doesn't carry it's own weight, especially not for small projects.


I can't tell you if you will like it, but based oh how much you agree with or disagree with my logic, it may sway your opinion.

I came from C++ -> JS -> TS, here's my experience and thinking.


Typing disciplines

C++ is statically typed and weakly typed; weak typing can cause bugs every once in a while, but static typing allows one to define strict APIs, and removes the would-be runtime checking code.

JS is dynamically typed and weakly typed, it coerces stuff so that it it "just works". It's crap, Python seems better, considering how it has strong typing.

TS is both statically typed and strongly typed. TS is supposed to have no runtime overhead compared to JS.

What's TypeScript's secret, you may ask?

C++ and JS have runtime type casts... TS does not.

TS merely describes JS as-is, that means crap JS => crap TS.

Plain and simple.

If you write the following C:

y is a char, runtime instructions are emit to truncate the int to a byte.

JavaScript:

y is a string, a runtime lookup is done against the local scope and then the globalThis object in order to even find String, but then it's called and converted.

TypeScript:

y is a TypeScript string, but it's a JavaScript number.

Yup, your probably thinking, "wait, wtf?".

TS forces the developer to manually perform type checking and such, the compiler merely attempts to describe what you're doing.

TS has type assertions, as shown above, which are common, they emit no runtime code, yet somehow manage to be worse than a C reinterpretation cast.

Besides this, TypeScript's type system sucks. TS uses a "structural type system," no nominal typing whatsoever.

TS is full of weird, unintuitive, edge case stuff, here's an example:

Perfectly fine, compiles cleanly.

Here, we used an object literal to describe an object with no keys.

false isn't an object, so what gives?

Did I mention structural type system?

{} means a value that can be accessed via property access, and on which lookups will be done on no keys. This is actually an empty interface.

Here's what would happen if you looked up "bar" on some random values:

The above API merely prohibits null and undefined, as they are not object-ish values, and would thrown an error.

Other nice examples of how this works:

Another problem is that TS's type system isn't comprehensive, nor does it go beyond JS's own type system.
For example, JavaScript only has f64 number types, yet all browser functionality runs through a different type system known as WebIDL (which is based off of C++). It has more specific types, such as unsigned integers. For example, a browser function that takes a u32 might be declared like so:

How does TS type this?

TS won't care if you pass in -4.5 and get some funny bug as a result, this is because TS has only as shallow of an understanding of the function as JS itself does.

Have fun!

(Honestly, idk wtf that will do, I think it'll convert to an integer via float truncation, then reinterpret/wrap as a u32?)

(Update: it throws a TypeError)

Then, there are some JavaScript things that cannot be properly typed in TypeScript, here's an example:

This is because all objects are blindly assumed to inherit from Object.prototype.

As an added bonus, TS has decided that a lot of otherwise fine JS is unsafe, therefore must not be allowed, here's an example:

Here's what you get for attempting to compile it: playground link.

(I'd say that TS already has as many wtfs as JS itself)


Oh yeah, and I almost forgot!

This is totally valid TS too:

If you like JavaScript's type coercions, you shouldn't mind ;)


Features

So, maybe you want some features that a higher-level language should provide?

Go away, there aren't any.

Here's TypeScript's design goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals#non-goals

Provide additional runtime functionality or libraries. Instead, use TypeScript to describe existing libraries.

Nothing emits runtime code, nothing is even restructured.

The only two things that I can think of that had broken this were class declarations and namespaces.

Namespaces emit awful JS anyways:

Emit JS:

And the best part about namespaces is...

that they're practicially deprecated anyways:

TypeScript won't provide any features higher-level than what JavaScript already provides you.


Language values

I as an individual, love the idea of a program failing to compile, rather than executing and encountering a runtime exception or otherwise failing.

Alongside this, one of the things that I care about a whole lot is time. I hate slow stuff, plain and simple. I hate waiting 4 whole minutes for a single file to compile, to just fail compilation anyway.

I don't like slow development time, especially not without something offsetting the loss in time.

(What is the point of waiting for a typechecking compiler, if it fails to even emit a correct program?)

TS is the slowest "compiler" I've ever used in my life.

I can't exaggerate this, C++ compiles faster, Java compiles faster, Go compiles faster... you get where I'm going with this.

TS still has as many JiT bailouts as plain JavaScript. TSC is written in TS, it's slow.

So, it needs to pay for the loss for me to consider it... it really didn't.

TS as a language doesn't genuinely care about preventing compilation of programs that are provably correct. I'd say this this guy did a good job at summing up this problem: https://github.com/microsoft/TypeScript/issues/28682#issuecomment-822590859.

Now, I'll admit, when you have a 100+ file project, any typing is better than nothing, but at that point, the compilation process will be slower than all of Chromium's C++ :)

Writing TS, yet backing it by real conversation code really helps write correctly functioning code, for example:

I'd say that this is because having code that actually receives correct types is far more valuable that whatever broken contract TS will provide. Solely using TS will not fix the underlying JS.

I've already really just been ranting at this point, by my last example of how amazing TS is:


Conclusion

You end up writing JavaScript, with extra overhead.

Alternatives

Dart, it can be run in the DartVM, compiled to native code, or transpiled to JS (i.e. make a server, make a mobile app, or make a webpage). It also isn't half as slow as TS to compile, executes faster, has a sensible type system, and has optimization settings.

https://dart.dev

DynamicSquid

@xxpertHacker This is a beautiful rant I love it :D

With Coder's answer, he approached TS from a JS viewpoint, but Moca you're not a JS dev. With Hacker's answer he approached TS from a C++ viewpoint, which is more similar to your viewpoint Moca. So I would trust Hacker's viewpoint more.

If you want to get into networking, you can do it in C++, but for a high level language, I think Python or Crystal would work fine.

realTronsi

@DynamicSquid honestly imo it's not a matter of viewpoint but rather more of preference. It has pros and cons, and it's generally just how you weight each pro and con to determine whether you like ts or not, though generally I would say anyone with a functional brain would not choose typescript (sorry coder, but I mean cookies don't have brains so uh yeah).

xxpertHacker

@DynamicSquid I actually didn't want it to come off as "ranty" as it did, but halfway through, I realized it was too late.

On mobile I can't enlarge the textbox, so I can only see the last 2.5 lines. I'll touch up the answer on desktop soon.

Update: Done.