Today I Learned - Rocky Kev

TIL how to explain Typescript like i'm 5

POSTED ON:

TAGS:

Typescript is quickly becoming a powerful tool for Javascript developers. Typescript provides optional static typing.

It lets you:

I really like this reddit comment that ELI5:

The thing you have to remember is: TS is not a strictly typed language. It attempts to emulate it, but because JS is valid TS, you can break types whenever you want. It's only as strict (and therefore as useful) as you make it. It's why good projects turn strict on in the tsconfig and do not allow any. Even one any will break the chain of custody, because the TS is being asked not to worry about it and blindly assume all's well. It's no longer useful as a check.

Say I'm a giftshop, and I want to sell gift-wrapped bananas. We have full end to end checks to be sure we're dealing only with the best bananas modern science can grow. They never accidentally get swapped out with other fruit. We usually do the same with the giftwrapping but sometimes we ask you just trust that this giftwrapping is legit. Joe's been giftwrapping bananas for decades. He knows what he's doing. On paper, all looks good! Banana checks all the way, giftwrapping checks usually except some areas where we ask you trust us. CI says green, so ship it!

Suddenly I start receiving bananas in lockboxes, bananas wrapped in seven layers of packing tape, or even live gorillas holding bananas. Where do you think the problem occurred?

Does this restrict the libraries you can use? Cause some don't have TS? Absolutely, and in 2022 this feels easy to do, honestly. I do not want a library that can't be bothered to publish their own types. I cannot trust them.

Many companies even say TS isn't enough. We need GraphQL or swagger to type our services as well, so consumers understand the shape of data they're getting. This is a good thing.

The perils of not having types is expressed in JS in many ways actually. Its very loosey goosey with numbers and strings, because it's trying never to fail. It assumes this might happen because there's no types and JS was originally made in just a few days. But when your data is really important, an attitude of "JesuS take the wheel" doesn't cut it.

via zephyrtr, with the original post Typescipt is making me want to quit my job and become a farmer


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.