The solution we have put together now allows TypeScript to take a look inside of each of our different
cases and make sure that we are using the action in an appropriate way to, for example, inside this
search repository success case right here, TypeScript knows that the action is of type search repository
success section, which means it has a payload property that is an array of strings.
If we take that payload property and tried to assign it to air right here like so we'll get an error
because now TypeScript is saying you are trying to take an array of strings and assign it to a property
that is supposed to be either a string or not.
So this is pretty good.
We have added in some reasonable amount of type checking inside of our reducers.
There's just a couple of kind of usability issues inside of you.
Now, first off, adding in this gigantic type annotation for our action argument is going to get pretty
tedious really quickly.
So another way that we might decide to write all this stuff out is right above our producer say type.
Action is going to be and then copy over really cut and paste this kind of or really it's a type of
union is what we call this.
So I'm going to cut all that and paste it in like so.
So we've now got a new type inside of application called action.
And this represents all different possible actions that can be processed by all of our different reducers.
Not going to use that at action like so.
And that's pretty much it.
The other thing that I would really like to somehow improve just a little bit, just like inside of
a normal redox JavaScript project, we have now duplicated some strings instead of reducers and up inside
of our interfaces as well.
And whenever we start to put together our actual action creators, we're going to need to use these
same strings over inside those action creators, too.
So I think that duplicating all these strings and all these different locations probably not a good
idea.
So to handle this in a slightly better fashion, once again, right above the reducer.
I'm going to add in an enum called action type.
If you're not familiar with an enum, it's essentially kind of like an object.
It sets up a variety of different properties that all have a very closely related definition, are very
closely related, meaning.
So inside of our enum, we're going to list out all the different possible action types that we're going
to have across all different actions, all different action creators and all different reducers.
So inside of here, I'm going to write out search repositories.
And if you want to be like really reduc see here, we would kind of put in search repositories all capitals.
Like, so it is not required to do all capitals.
Again, this is kind of convention and I'm going to set that equal to search.
Repositories come at the end.
Well, then repeat that for our other two action types.
So search.
Repositories.
Success.
And then finally search.
Repositories, air.
Now, again, just a little bit more on items, if we ever want to refer to these different properties
and access the strings we have assigned to them, we would do something like action type search repositories
or action type.
Search repositories success, so now we can use this enum to access all these different action types
rather than writing out the raw strings, which again is usually bad because we're just duplicating
these strings all around and it's very easy to make a typo inside them.
So to make use that, we could then go down to our switch statement and replace all these strings with
something like action type dot search repositories.
Action type search repository success.
And then action type search repositories, er.
Most are going to clean up that comment right there, because I was just there for informational purposes.
OK, that's definitely a good improvement.
Now, the last thing that we can do.
Is referred to the Inam for finding the actual values of these strings inside of our three different
interfaces as well.
So, again, rather than hard coding in these strings to these interfaces, for the first type, we
could put in action type thought search repositories.
Type dot search repositories success.
And action type search repositories, air.
All right, that's pretty much it, so this file right here is a really good example of how to set up
a producer, how to type our different actions.
How to apply those action types inside of reducer, how to set up an action type enum and make use of
that inside of a reducer?
And our action type interfaces as well, so this right now is a pretty, pretty good example.
I'm pretty happy with this.
The one improvement that I would like to make to it is to extract these interface definitions and extract
the action types enum into separate files just to make sure that we can reuse this enum inside of other
reducers very easily and also make sure that we can access this inside of our different action creatures
as well.
We will eventually want to also make use of these interfaces inside of our action creators to.
The last thing we're going to do is take all the stuff that we have now created and split it up into
a couple of different files and folders based upon exactly what part of our redux side of things it
actually implements.
So let's take care of this little reorganization in the next video.