Hacker News new | ask | show | jobs
by a_humean 1244 days ago
Fortunately ES2015 was 7+ years ago, so no I won't. :)
1 comments

Every few years I'll jump into JavaScript for one thing or another, and recently I jumped back into TypeScript and holy cow it's shaping up to be a really nice language. I really like `const` as well as TypeScript's concept of type widening/narrowing (`"foo"` is its own type as a specific string but can be widened to `string`), which allows the compiler to know that `document.createElement("table")` returns an `HTMLTableElement` rather than `HTMLElement`--this could have been avoided by having a `document.createTableElement()` method with its own signature, but given that it has to work with older, dynamically typed APIs, this is a pretty elegant solution.

Similarly, if I have a discriminant union `type FooBar = "foo" | "bar";`, TypeScript seems to know that `if (["foo", "bar"].includes(x)) {...}` exhaustively handles all permutations of `FooBar` (no need for a `switch` statement with explicit arms).

The static typing really helps me avoid a bunch of "undefined is not a function" stuff that I would waste time with in JavaScript.

Pretty cool stuff!

TypeScript's type language is extraordinarily powerful. It's completely reframed the way I do web development; I tend to do much more functional and less method-based semantics these days because interfaces and generic types make that feasible without going mad from losing track of what functions can be applied to what data (and receiving no help from the very lax type semantics and runtime of regular JavaScript).