Live data from Hacker News

Tricks I wish I knew when I learned TypeScript

cstrnt.dev

271–276 of 276 posts

Re: Tricks I wish I knew when I learned TypeScript

#271

Earlier quoted context omitted.

Did anybody else have their brain do a weird backflip seeing `Array.isArray[0]`?? Object.getOwnPropertyDescriptors(Array.isArray) /* { '0': { value: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray', writable: true, enumerable: true, configurable: true }, length: { value: 1, writable: false, enumerable: false, configurable: true }, name: { value: 'isArray', writable: fal…

How did you get that result? I only see length and name.

Well, it was a kind of a joke intended for the folks whose brain saw the comment the same way I did. I got the result by applying the footnote to the object, as half-suggested by syntax of the parent:

  Array.isArray[0] = urlString

Re: Tricks I wish I knew when I learned TypeScript

#272
post #231
post #122

I'm so happy powerful type systems are more popular now. TypeScript bringing a great type system to a language as popular as JS is fantastic. Rust is also a great way to get a great type system while staying in a systems programming and procedural environment. Hell, even python type annotations support union types. I never knew the depth of type systems until the last year when I took a type theory course and a compi…

What course?

It was at my university unfortunately, but the content is likely covered by any course labeled as "programming language theory" or something similar.

Re: Tricks I wish I knew when I learned TypeScript

#273
post #88

Earlier quoted context omitted.

Probably best to just lift one off of a major library like Lodash, they're well tested and efficient (no need to actually use the library, do include the LICENSE somewhere though): https://github.com/lodash/lodash/blob/master/isObject.js But depends on your needs, and you can also attach a typeguard to it.

Why not use the library? With tree shaking and/or direct imports you will ensure the same bundle size as if you just copied the file, and you don't have to worry about licenses etc. In fact, since other dependencies might depend on lodash you can deduplicate the import and actually save on bundle size. You'll also get notified of any security issues in your lodash imports if your CI pipeline is setup for doing that k…

Mostly if you look at it out of the context of a single function - a lot of projects end up taking a huge number of dependencies, with a lot of overlapping functionality, because you used one function from this one, another function from that one… I’m fine with using libraries when they actually do heavy lifting that’s core to an application, but a single two line function requiring including hundreds of unrelated irrelevant ones? That will impact the coding style of your team and does have security downsides, like needing to trust the library authors and potentially breaking your build because they changed their APIs or deleted a package or whatever. Copying a 2 line function has very clear boundaries to what it can and can’t do, and doesn’t hide the internals of what you’re doing behind the mystique of “an external dependency”.

Re: Tricks I wish I knew when I learned TypeScript

#274
post #61

Earlier quoted context omitted.

Which "const" do you mean? The one in front of a variable declaration cannot make the array itself constant. That's because that "const" only refers to that variable itself, which is just a pointer (except for the primitive types). The variable declaration "const" means this variable cannot be changed to point to a different object. It says nothing about the thing it points to and that is how that keyword was designe…

This comment would be a lot shorter if you assumed I meant Typescript in response to a Typescript article... But I digress, the point is in my experience Typescript is very good about catching footguns left around by ECMAScript. So I'm surprised there isn't some sort of catch for this as written in the article maybe behind a config flag, not by rewriting the definition.

> if you assumed I meant Typescript in response to a Typescript article...

You misunderstand TypeScript.

They cannot 8and will not) change the "Javascript" in Typescript. "const" is a keyword with a meaning defined by the ECMAscript standard.

Typescript IS Javascript. All they do is add type annotations. Only some old non-essential features like namespaces and enums need to be transpiled, and "enums" really is not much and should actually be handled by whatever minifier and bundler/packager you use. Other than that, if you removed the type annotations you are left with 100% ECMAscript.

Typescript was meant to be just a type-annotation extension and explicitly made the decision that the code itself would always be stock-standard Javascript.

Arguably, it was a bad design decision that now confuses lots of people about the nature of Typescript by bundling type-checking and transpiling to some target (originally for older runtimes that did not understand es2015 or were lacking some feature available in the latest JS runtimes and ECMAscript standard).

It is therefore not a surprise at all that Typescript did not make "const" into something else. The basis always is the ECMAscript standard.

Re: Tricks I wish I knew when I learned TypeScript

#275
post #26

I actually don't really like Record types in the way people/library maintainers often use them - the type-checker asserts that values are actually present for all the specified keys, which is fine if the objects with the Record type really were exhaustive; but instead I often see them used where the reality of the data is a Partial - some keys are missing. Something about the abstraction causes people to misuse it fr…

You pretty much always have to define your record type with `| undefined` tacked on to the value type parameter. With that, the problem mostly goes away.

Yeah, but I use libraries that don’t do that, and have teammates that don’t care about this
Post reply on HN