Transcript from the "Recursive Types" Lesson
>> 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: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.