Welcome to my PinkLetter. A short, weekly, technology-agnostic, and pink newsletter where we cultivate timeless skills in web development.
You may not know how to use TypeScript if you come from dynamic languages.
Microsoft advertises TypeScript as “JavaScript with syntax for types.” And it’s the worst punchline they could use.
I mean, TypeScript is a superset of JavaScript: you can take any .js file and throw it at the TypeScript compiler. So they are technically right.
However, that marketing message produces the worst possible outcome: developers write JavaScript first and then add static types later as an afterthought.
In other words, a huge chunk of the TypeScript community doesn’t understand the why of static types.
The other day, I was working on some code and got annoyed at the fact that fetch
‘s json()
returns Promise<any>
and not Promise<unknown>
.
So I embarked on a journey and found some comments on GitHub that terrified me.
For context, at some point, they explored using unknown
instead of any
in node-fetch
. Here’s an issue a developer raised:
The main difference between any and unknow is that unknow you cannot treat it as an object whereas any you can treat it … like anything.
It is no problem. if you work with Javascript, but when you work with Typescript it brings problems (added pictures in spanish)
The solution is simple
json(): Promise<unknown>;
tojson(): Promise≤any>;
unknown
is problematic when you work with TypeScript but not with JavaScript 😱
And yet another issue:
this is unusable in typescript. v3.0.0 types the return value of await res.json() as unknown which means “this is dangerous info, you have no idea what it is. don’t touch it to avoid runtime errors.” but the point of a fetch request is to get the information back. you have to touch it. the type should stay any.
unknown
is for dangerous info that shouldn’t be touched to avoid runtime errors 😱
Luckily, some support unknown
in those conversations, including an issue in the TypeScript repository:
Since the new unknown type was added, it makes sense to replace a lot (if not all) usage of any in the definition files with unknown.
unknown
s all the way 🔥
Static types are a foreign concept if your background is in dynlangs. I know because I was there before exposure to Elm, PureScript, and Haskell. But let’s make a promise: we will try to learn and teach each other how to make good use of static types in TypeScript.
By the way, I tried to summarize the above in a couple of tips:
Why You Can’t Find Your Passion by Leon Hendrix
You can’t find your passion because you’re currently believing certain lies that you’ve been told. I could’ve saved years of my life if I understood what I’ll share with you here so you can potentially figure out yours (quicker).
Riccardo: This won’t help you find answers, but it will shook you with profound questions. Thanks, Tom!
The unreasonable effectiveness of polymorphic records by Clément Delafargue
Let’s have a look at a few patterns combining records and polymorphism. It should be fun!
Riccardo: This uses some dark Haskell magic, yet it’s clear and actionable. By the way, I’m always happy to teach some Haskell if you are a bit lost.
The 7 Pillar Developer: A holistic approach to building an exceptional career by Cory House
As developers, we focus on code. Yet mastering syntax alone isn’t sufficient. Our behaviors away from the keyboard profoundly effect our impact and career satisfaction. So, to dramatically expand our potential as developers, we must reconsider our behaviors as a humans. In this story-oriented session, we’ll explore seven surprisingly relevant facets of life that profoundly impact our software quality.
Riccardo: This will re-center you in someway. I promise.