Loops with Recursive Types
In our journey to learn the language of types, we've already gone pretty far. In the previous chapter, we started computing types from other types using conditional logic. We used the type system as a programming language for the first time and in this chapter, I want us to explore this idea even further. After code branching, it's time to learn how to write type-level loops!
Loops make programming feel like magic sometimes. It's a wonder that computers can solve such complex problems so quickly given the right algorithm. Computers just excel at repetitive tasks and loops let us tap into their power. There is no reason not to take advantage of this at the type level too!
In this chapter, we will use recursion to loop over tuple types. If you aren't already well-versed in recursive algorithms, the code I'm going to show may look unfamiliar. Bear with me though, and remember that we are not only learning a new programming language but a functional one! It takes time for all of these new concepts to sink in, and being open to some discomfort is the key to making progress.
Let's dive into it!
Two styles of loops
Most programming languages we use today are spiritual children of the C language. They have a similar syntax and are grounded in an imperative programming style. In the imperative style, we tell the computer what to do in a series of instructions that should be executed sequentially, in the right order.
Support Type-Level TypeScript!
Become a Member to join a community of 1500+ students and get access to all existing and upcoming chapters!
You will find everything you need to become a TypeScript Expert — 11 chapters of in-depth, unique content, and more than 70 fun challenges to practice your new skills.
Full access to all 11 chapters
70 type challenges with explanations
Lifetime access to all course materials
Exclusive discord community
Loading...