1

Type-Level TypeScript — Introduction

 1 year ago
source link: https://type-level-typescript.com/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Made with ❤️ by @GabrielVergnaud

Learn how to unleash the full potential of the Turing Complete type system of TypeScript!

Introduction

Type-level TypeScript is an online course to take your TypeScript skills from intermediate to advanced. It will give you a solid understanding of the type system's fundamentals and guide you through its most advanced features. You will find everything you need to become a real TypeScript Pro — not only in-depth content, but also fun challenges to practice your new skills, like this one right here.

Let's see if you can solve it!

Challenge
Solution
 * Try assigning "World" to `type Hello`!
type Hello = "...";
// Type-level unit tests!
// If the next line type-checks, you solved this challenge!
type test1 = Expect<Equal<Hello, "World">>;

Congratulation! 🎉

Over the years, the type system of TypeScript has grown from basic type annotations to a large and complex programming language. If you have ever looked into the code of an open source library you may have found types that looked intimidating and foreign, like some esoteric language coming from another planet. Library code often needs to be much more abstract than the code we are used to writing; that's why it makes extensive usage of advanced TypeScript features such as Generics, Conditional Types, Mapped Types or even Recursive Types. I personally learned these concepts while working on TS-Pattern, an open-source library that has the particularity of being extremely hard to type. In this course, I hope to share what I've learned by reading too much source code and tinkering for hundreds of hours with the type system.

Types are awesome for many reasons:

  • They document the code.
  • They make developers more proficient by providing them with smart suggestions.
  • They catch mistakes and typos.

The more the type system knows about your code, the better it is at helping you! Once you're fluent in the language of types, everything becomes possible. You will no longer feel like the type system restrains your ability to write the abstractions you need.

Want to check if your route parameters are passed correctly? Here you go:

// ✅ this is correct 👌
navigate("user/:userId", { userId: "2" });
// ✅ Looks good! `dashboardId` is optional.
navigate("user/:userId/dashboard(/:dashboardId)", { userId: "2" });
// ❌ `userId` is missing. Add one to fix the error!
navigate("user/:userId/dashboard(/:dashboardId)", { dashboardId: "2" });
// ❌ `oops` isn't a parameter. Remove it to fix the error!
navigate("user/:userId/dashboard(/:dashboardId)", { userId: "2", oops: ":(" });
// 👇 Scroll to see how this works!
// 🤫 Here are the kind of things you will soon be able to do!
type ParseUrlParams<url> =

This isn't magic. The type system is just a real programming language!

To get better at this, we first need to acknowledge that TypeScript's type system is a full-fledged programming language in itself! It's more than worth learning its fundamentals, just like we would with any other new programming language. Throughout this course, I'll try to show the correspondence between programming concepts you already know like code branching, variable assignment, loops and data structures, and their type-level equivalent. By assembling these building blocks, you will be able to create powerful type-level algorithms that make sure the abstractions you create are always used properly in your entire codebase.

Let's take your TypeScript skills to the next level!

Finally, we will see that moving more of the complexity of your code to the type level is a trade-off that isn't always worth taking. We will talk about compile time performance, type errors, type-level debugging and other challenges of type-level programming. I hope this will help you reflect and take the right decision when using these techniques.

It's worth noting that this course isn't a collection of TypeScript tricks! I believe that building a good mental model of the fundamentals is more empowering because it will help you solve problems we haven't covered. Knowing a few tricks can only get you so far, but mastering the building blocks of the language and the way they interact together will enable you to solve your real-world problems.

Let's get started!

Next ⟹

1. Types & Values


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK