Hacker News new | ask | show | jobs
by throwaway90650 1244 days ago
Try it with var
2 comments

s/var/let/g and you're good. This is the entire reason "let" was added in the first place, to correct this historical mistake. It's supported pretty much everywhere even by extremely conservative standards, and it's even the same number of letters. Literally no reason not to use it.

I personally dislike JavaScript. Even modern JavaScript. But this is a bullshit reason to hate on JavaScript.

Fortunately ES2015 was 7+ years ago, so no I won't. :)
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).