Now that we've got these three different interfaces, we're going to use them inside of our reducer,
so little bit of typing here.
I'm going to go down to my action argument and I'm going to update its type annotation.
We don't really have an interface called action anymore.
So I'm going to remove that.
And I'm going to say that whenever we receive an action, we know for 100 percent certainty that that
object that we receive are really the argument that we receive for action right here is going to meet
or satisfy the interface of search repositories.
Action or this one.
Or this one.
So to properly annotate action as saying it's going to be one of these three different interfaces,
I'm going to put down the first one and then a single pipe right there and then the success.
And then the air.
Like, so I'm going to save that automatically kind of collapse, is it to be like so?
So now we are being told right here that whenever we call our reducer, we're going to have this action
argument and it will be of type search a positive reaction or success or air.
It's now down inside of our switch statement, you'll notice.
We're not getting any errors here.
And if I mouseover say action, not pela right here, I am told that this will be an array of strings.
And if I mouseover the air property or where we assign air for the air case by mouseover action, not
payload, you'll notice that says that it is a string.
So what exactly is going on inside of your.
Well, I want to remind you about a topic inside of typescript called Type Guards', if I go right above
my switch statement and I put in just action and then mouseover typescript at this point in time thinks
that this action argument is going to be of type.
Repositories action or success or air, which is 100 percent accurate, but at some point in time,
we want to kind of narrow down that type of action.
We want to figure out if this is an action that is a repository, is action or success or er to do so,
we can write out a little if statement and we can say something like action type equal to let's try
search repositories success.
So this is functioning as a type guard, if this if statement is true and we end up inside of it, then
we know with 100 percent certainty that this action argument that we just received, this action object
right here, must be an object satisfying this interface, because only this interface is saying that
our object has a type of search repositories that success.
So inside of here, we know with 100 percent certainty.
That action satisfies the.
Search repositories, success action interface.
And because of that, because it satisfies that interface, we know further with 100 percent certainty
that it's Pelloux property exists, it does have a payload property and that payload property is an
array of strings.
So down here, if I now do action, payload and mouse over, that typescript says, yeah, this is definitely
a success action and that payload property is definitely an array of strings.
So this is the real key to how we are going to write out our reducers whenever we put in this action
argument, we're going to somehow say that the action argument is one of all these different kinds of
actions that we have inside of application.
We're going to define a separate interface for every kind of action we ever put together.
Then inside of our reducers, we can use type guards like this to figure out what kind of action it
really is and help TypeScript further understand what kind of action it really is.
But obviously, we're not really writing out this if statement.
It turns out that switch statements function as type guards in typescript as well.
So TypeScript is 100 percent aware that if we go into the switch statement and the actions type is equal
to, say, search repository success, then inside this case we are just like above in the if statement,
100 percent certain.
That action is Zorch repositories.
Success action, and again, that means that our action has a value property that is an array of strings.
So this is a key and again, this is like a really tough thing to understand.
So I'm going to undo that statement and a little action right above.
We're going to take a quick pause and continue in just a moment.