A look at recursive conditional types, template literal types, and more

If you’ve ever read any of my TypeScript articles, you’ll know that I’m a big fan of mapped types. They’re my all-time favorite feature because they’re fun and easy to use once you get the hang of them.

Mapped types landed in TypeScript version 2.1, and since then, they’ve seen more and more improvements. If you don’t know what mapped types are, I suggest reading my previous article.

In this article, we are going to explore some of the features introduced in 4.1 and how you can apply them to mapped types.

TypeScript 4.1

TypeScript 4.1 was released recently and there are many goodies in it. In this article, we will especially focus on:

  • Recursive conditional types
  • Template literal types
  • Key remapping in mapped types
  • Checked indexed accesses

We will dive deep into those and see what new things we can do.

I strongly recommend using the TypeScript playground to best follow along with this article. You can tweak all the configurations and even switch the TypeScript version. It’s a must-use tool for newcomers, as it will also show the output of the JS code.

#programming #typescript #web-development #javascript

TypeScript 4.1’s Advanced Mapped Types
2.60 GEEK