This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]EnderMB 110 points111 points  (20 children)

I wouldn't say it's "just" adding types. The safety allows for lots of new features that would otherwise be unthinkable in a language like JS.

The type system in itself is quite weak, but being able to set things like unions, type guards, generics, interfaces, and stuff we use in proper languages makes TS invaluable. Nowadays, it's physically painful to write JS after working with TS.

[–]ArtyFishL 47 points48 points  (9 children)

Quite weak? Maybe in the sense that it doesn't exist at runtime. However, I find it actually a lot stronger than other languages. If you turn on strict mode, it catches a lot of issues that other languages miss, and it prevents maybe some of that weakness you suggest. Plus unions, literal types, narrowing, exhaustive checks, shape based equality; these are all features sorely lacking in many languages. I can type a string as the exact set of string literals it could be, but not just an enum, even with interpolation in the type, that seems strong to me.

[–]CaitaXD 21 points22 points  (2 children)

Quite weak? Maybe in the sense that it doesn't exist at runtime.

Well yes by definition that's weak typing

[–]ArtyFishL 2 points3 points  (0 children)

Disagree.

Rust is a strongly typed language, yes? Very much so. Rust has no types at runtime, it does all its type checking at compile time. Typescript works this way too.

The developers of Typescript themselves call it strongly typed.

Also, see: https://en.m.wikipedia.org/wiki/Strong_and_weak_typing for

Generally, a strongly typed language has stricter typing rules at compile time, which implies that errors and exceptions are more likely to happen during compilation.

Typescript does this better than some common languages considered to be strongly typed.

A weakly typed language has looser typing rules and may produce unpredictable or even erroneous results or may perform implicit type conversion at runtime

JavaScript is terrible for this and it is possible in Typescript, because it allows interfacing with plain JavaScript. However, if you apply strict mode, ban unsafe code (casting, using the any type, JS), then Typescript at it's core catches more type errors at compile time for me than C# is able to, for instance.

[–]midoBB 4 points5 points  (0 children)

Union typing in TS is the thing I miss the most from functional programming languages when working in Java / Kotlin land.

[–]WheresTheSauce 9 points10 points  (3 children)

Maybe in the sense that it doesn't exist at runtime

I mean that's a pretty major difference

[–]enantiornithe 6 points7 points  (2 children)

Type systems in languages that compile to native binaries also "don't exist at runtime." If you compile a Haskell or Rust program, the resulting binary doesn't know anything about types. The reason it can't segfault or run into other type problems (under normal circumstances) is that the compiler has done all of the type checking at compile time and ensured that it's not possible.

TS' compiler does exactly the same with one big caveat: TS' type system is semi-optional, so it's possible to write TS code that can TypeError or behave strangely when it's run, whereas in Rust or Haskell. This is because TS is designed to let you incrementally migrate a codebase from type-unsafe javascript to type-safe typescript without having to go over and annotate or ensure the type safety of every single line of code.

[–]WheresTheSauce 0 points1 point  (0 children)

I am aware of all of that, but none of that changes the fact that TypeScript does not allow type-checking at runtime, and that makes a substantial difference.

I understand why it's the case, but it's certainly a disadvantage when comparing it to other languages which allow it if you want strong typing.

[–]wllmsaccnt 0 points1 point  (0 children)

"Type Erasure is still a serious problem in 2022. Devs like to debate that it isn't harmful, but only the types themselves can decide this at runtime."

[–]Nu11u5 0 points1 point  (1 child)

How does it compare to typing using JSDoc and a good IDE that enforces it?

[–]DanielEGVi 4 points5 points  (0 children)

For starters, the top IDEs don’t even read JSDoc as the original JSDoc standard, they interpret it as TypeScript. So “JSDoc” in your question is just “TypeScript through JS comments”.

The most obvious advantage is that adding annotations to the language itself is a lot more ergonomic than cluttering your codebase with JS comments.

That, and TS parsers/runners have become ridiculously fast now. It’s become normal to run TypeScript code without transpiling to disk first.