Ask coding questions

← Back to all posts
Is TypeScript worth it?
h
MocaCDeveloper (713)

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?

Answered by xxpertHacker (931) [earned 5 cycles]
View Answer
Comments
hotnewtop
xxpertHacker (931)

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:

int x = 0;
char y = x;

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

JavaScript:

const x = 42;
const y = String(x);

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:

const x: number = 377;
const y: string = x as string;

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:

function print_an_object(
    data: {}
) {
    console.log(data);
}

print_an_object(42);
print_an_object(false);
print_an_object("this is an object ;)");

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:

false.bar; // undefined
(36).bar; // undefined
null.bar; // uncaught TypeError: ...

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:

type T = {
   x: number
};

const a: T = {
    x: 2,
    y: null
};

const b: T = {
    x: -0.1,
    z: null
};

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:

[Exposed=Window]
interface foo {
    static undefined bar(unsigned int value = 0);
}

How does TS type this?

declare namespace foo {
    export function bar(value?: number): void;
}

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!

const x: number[] = [ 4, -9, 0 ];

x.length = -920472.435;

(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:

const x = Object.create(null);
const y = x.toString(); // uncaught TypeError: "undefined" is not a function

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:

class T extends Number {
	x: number = 0.0;

	constructor(n: number) {
		const temp: number = n ** n;
		super(temp);
	}
};

console.log(new T(10.0).valueOf()); // should be (100.0)

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:

function to_string(x: number): string {
    return x + " hi";
}

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:

namespace foo {
    export function bar(value: number): void {}
}

Emit JS:

var foo;
(function (foo) {
    function bar(value) { }
    foo.bar = bar;
})(foo || (foo = {}));

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:

function instances_of_char_in_string(
    str: string,
    char: string // no char type, amazing, huh?
): number {
    // Runtime code to cast
    str = String(str);
    char = String(char)[0];

    let sum = 0;

    // Do something here

    return sum;
}

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:

const char: string = "123"[5];

console.log(char); // undefined; good job TS, my code works as intended

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 (5024)

@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 (923)

@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 (931)

@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.

xxpertHacker (931)

Btw, TS is merely used for the same things that JS is used for. TS is a transpliled language, it's "compiler" is a source-to-source compiler which just emits JS.

Coder100 (18906)

it is very worth it.

You will be writing much better js code, it prevents you from doing things like this:

let a = null;

function get_key(obj, k) { return obj[k]; }

get_key(a, 'a'); // oh nooo

not only that, but you have access to namespaces, enums, and interfaces! All very common static language features that actually are useful.

Not only that, but you can use es6 modules without having to add type="module" -- everything is all precompiled!

Also autocomplete
Also docs
Also most editors support typescript, so you are making coding better for everyone!

Downsides

a bit verbose according to some, and not useful for anyone who doesn't even use js that much??? Also a lot of set up.

Clarification

Typescript is a superset of js, you technically aren't going to learn anything new. Also, it's not slow??? Maybe it compiles a bit slower, but using tsc -w (automatically compile on save) makes compiling almost seamless.

Because it is a superset, you may be writing better and maybe faster js code.

Final advice

guys play my bot cycle plz made in ts owo
https://github.com/cursorweb/Cycle-Bot-Game

SixBeeps (5335)

TypeScript is just an extension of JavaScript, much like how C++ is an extension of C.

Should you learn it? Maybe. It could help clear up your code and prevent type confusion, and some libraries have started to choose it over regular JavaScript.

(I didn't know it at the time, but TypeScript was actually my first object-oriented language. It was used in Unity as one of the three major languages before C# took over.)

There are some other benefits, but if you know JS, learning TS takes minimal effort. You did mention that JS has strong bonds with web development, and you're not wrong, but there's plenty of other applications that it has. My advice would be to learn JS with a library such as Electron or Node and see if you like it. From there, move to TS if you feel so inclined.

Coder100 (18906)

ew unityscript

you truly are an og unity user
hats off to you! @SixBeeps

DynamicSquid (5024)

I don't have an answer for you, but if you ever decide to create a game engine, let me know :)

DynamicSquid (5024)

Also, just my personal advice (take it with a grain of salt), but as C/C++/low-level programmers, I would stay away from high level langs... I don't think they're worth it

realTronsi (923)

@DynamicSquid good luck making interactive HTML pages using C++!

DynamicSquid (5024)

@realTronsi I don't make interactive web pages, I make the web browsers and web engines that are used to make interactive web pages :)

xxpertHacker (931)

@realTronsi That's actually going to be a reality in the near future (< 5 years?), considering how it's already being done with Rust more and more often.

DynamicSquid (5024)

@xxpertHacker Hmm... you really think a language like Rust can compete with the current JS family of languages?

realTronsi (923)

@xxpertHacker javascript was built specifically for interacting with the DOM.

xxpertHacker (931)

@DynamicSquid No, I don't think that it can... a whole lot of others and I know it can ;)

Example

InvisibleOne (3218)

I'm not 100% sure, but I think that TS is just a library for Javascript, like Jquery, Processing or Node. As for how useful it is? That really depends on what you are doing. I would suggest that you learn at least the basics of JavaScript first and then try learning TypeScript.

Coder100 (18906)

no!!!! ts is not a library!!!! @InvisibleOne

xxpertHacker (931)

@InvisibleOne Node isn't even a library either.

xxpertHacker (931)

@InvisibleOne Yeah umm... it's a JavaScript runtime, providing an execution environment, an event loop, and it's own built-in APIs. :/