Check out a free preview of the full TypeScript 5+ Fundamentals, v4 course

The "Recursive Types" Lesson is part of the full, TypeScript 5+ Fundamentals, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Mike explains the concept of recursive types using an example of nested numbers in an array. Defining a recursive type using type aliases in TypeScript and how it allows for infinitely nested arrays of numbers is also demonstrated in this segment.

Preview
Close

Transcript from the "Recursive Types" Lesson

[00:00:00]
>> Let's talk for a moment about recursive types which you'll need in order to attempt the exercise that we're leading into. So let's imagine we wanted to type, values like this, right? It's an array of numbers nested. You can nest numbered arrays inside other numbered arrays. You can't put a string here, Because it's not number, but you can go as deep as you want.

[00:00:32]
So we would need a recursive type in order to describe this, and this is an example of a recursive type here. So we're saying nested numbers, can be a number or an array of nested numbers. As we're evaluating this type, we're first looking at the right hand side, just like a JavaScript expression, where if you say x equals 5 or x equals the result of this function, right?

[00:01:03]
Well, first we're gonna run the function and then we're gonna get the return value. And then finally, we're gonna assign it to the variable. Type aliases are processed in much the same way, where we're gonna look at the right hand side of the equals first. And so what's happening here is we're saying, all right, it's allowed to be a number.

[00:01:23]
It's allowed to be this type, which I know we're in the process of defining. So I'm going to put a pin in that, all right, now I'm gonna say, this is typed nested numbers and it sort of all fits together. It's almost like you can just reference the type itself here.

[00:01:39]
So this lets you say, numbers or arrays of numbers infinitely nested. This is an example of a recursive type, It's a little mind-bending. This is a simple example, but a little bit of an abstract one, but we're gonna need it in order to define types for JSON. So here, let's prove to ourselves that this works.

[00:02:07]
We can push 41, you can push an array containing the number 41 as many times as we want. But we cannot push the string. I wonder if we could push an empty array. I think so, it'll infer that it's a number array. It'll try to make it work, yep, that's fine.

[00:02:30]
It's like those are empty, I can see how they could be numbers, I'll treat them as arrays of numbers. It's trying to bend things in order to make it all happen. That's a recursive type. If you look online or if you Google this, you may come across outdated information that says you must use a technique involving interfaces and type aliases to make recursive types.

[00:02:59]
Used to be the case that you had to use both, have one feed into the other. You don't have to do that anymore, just type aliases will do.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now