This Crazy Syntax Lets You Get An Array Element's Type
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
TypeScript is taking over the world. It’s now the most popular language on GitHub, having overtaken JavaScript in the number of PR’s/week. But TypeScript can feel totally different to JavaScript.
The strange syntax, the arcane rules of inference, the generics.
It’s extremely tempting to just ignore the errors and slap on an ‘any’.
The vast majority of TypeScript application code out there is written under severe time constraints.
That’s how most apps get built - blood, sweat and ‘as any’.
As TypeScript gobbles up more and more market share, TypeScript wizards are going to be more and more in-demand.
Having the knowledge to transform any’s into safe types will save your team bugs.
This wizardry makes your skills even more valuable.
But it’s not just about cleaning up legacy code. Some wizardry under your belt lets you come up with wonderful, startling DX improvements that can accelerate your team.
Want a custom form abstraction? A type-safe REST API endpoint creator? A flexible Table component?
You’ll need to know some wizardry.
The more you can enable your teammates to develop faster, the faster you’ll rise up the ladder.
What’s more, it’s satisfying, engrossing work. You’ll be designing and implementing API’s that help folks code faster.
For example, If I were building an app where security was critical, I’d use the crap out of branded types.
Check out this Twitter thread I wrote on branded types (and give me a follow if you haven’t yet!)
Share this article with your friends
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
Learn how to publish a package to npm with a complete setup including, TypeScript, Prettier, Vitest, GitHub Actions, and versioning with Changesets.
Enums in TypeScript can be confusing, with differences between numeric and string enums causing unexpected behaviors.
Is TypeScript just a linter? No, but yes.
It's a massive ship day. We're launching a free TypeScript book, new course, giveaway, price cut, and sale.
Learn why the order you specify object properties in TypeScript matters and how it can affect type inference in your functions.