Instructor: Here I have a Redux reducer. Above here, I have the actual definition of the state. As you can see, it's very deeply nested. We have a few root level properties, but then in that, we have an array of todos, which is another complex object which actually has a link to another object.
In Redux, the state needs to be immutable. If there's an existing instance of the state, I should not be able to reassign any of its properties, or any of the properties of any of its substates, regardless of how deep down the tree we go. This includes not being able to reassign array values at specific indexes, or even the properties of objects at certain indexes in the array.
You can see that everything works fine in this case. One thing we could do to prevent this is we could start adding the read-only flags throughout the state, and mark all arrays as read-only arrays. Ideally, we want to avoid that. We want to define the state shape with as little overhead as possible. Only when it's applied in the context of a reducer, it needs to be read-only.
If we had some automated way to get from this type to this one, we could then focus on what the state actually represents, how to best structure it, and then let some automated system add all the read-only flags for us before it's actually applied to the reducer.
By doing that, we also ensure that whenever developers start changing the shape of the state, they don't forget to add read-only flags, because they'll be added automatically.
How do we actually transform one type into another? The answer to that is generics. I'll call my transformer deep read-only, and I'll first use map types to loop over every key of the type that is passed into here. For each key, I'll return the original type of its value, but then I'll add a read-only flag to it.
If I scroll down here and I replace the type of my state with a read-only state, I can see that my change actually took care of the first few properties in here. You might be tempted to just recursively apply this type again to each property, which will surely make it read-only even at the deeper levels. If we hover over it, we can see that it's complaining about it.
That might seem like it solved our problem, but it's also going to start messing with some of the types. For example, it won't let me map over arrays now, even though map is non-mutative.
We want to be careful and specific about how we mark each type as read-only. Let me rename this one as just for objects. Then I'll create a new one where I can start to be specific about how I transform each type. I'll call this one deep read-only, and it will actually be the starting point.
It's first going to check, is my type an array? If so, infer the type of its elements and return a read-only array instead by applying the deep read-only object recursively on each of its wrapped types. Otherwise, if it extends an object, apply the deep read-only object type directly. Finally, if it's none of those, it's a flat primitive, so just return T.
You'll notice the recursive loop here. I start with a deep read-only type. A title returns D if it's a primitive and attends, or it calls the deep read-only object type if it's an object or an array. The deep read-only object type goes through each key and, in turn, it calls the deep read-only type again with the type of that property.
This recursive loop is what's going to drill down the properties of objects and arrays, ensuring everything is read-only, regardless of depth. If I go back to my assignments here, I can see that it's complaining correctly about all of them. If I hover over them, I get a really useful message.
It's also not breaking the arrays, and it's letting me map over them, including correctly inferring what type each of their elements is.
Before we wrap up, let's go back to this statement over here, as it's a bit suspicious. It works great if T is an array of objects, as in the case of my todos array, because we're transforming an array of objects into a read-only array of read-only objects.
What if we have an array of primitive strengths, like we have for this property over here? How would that work? It turns out that using mapped type modifiers with primitives just returns the primitive itself, because there's no key to map over. You can also see that down here. That's why it lets me call to upper case on each todo.
It works for arrays of primitives and arrays of objects, but what if we have an array of arrays, like I have here? That will indeed take us to our previous problem and break the inner array, as that's the one we're sending to the object mapper. We won't be able to use array methods on it.
If you want to be extra careful and make it work, we can add another line here that checks for arrays of arrays, and converts it to a read-only array of read-only arrays. That's going to complicate matters a bit, so I'd be careful about adding this unless you really think you need it. If we look down here now, we can see that our mapping works correctly.