In this video we're going to start to add in several pieces of code to make sure that typescript can
make sure that whenever we create a listener we've got some matching subject and data type argument
provided inside the on message function.
So let's get to it.
The first thing we're going to do inside of our events directory we're going to create a new file and
inside there we're going to put together something that's going to list out all the different valid
subject names that we ever expect to receive.
So inside my events directory I'm going to make a new file called simply subjects dot.
Yes.
Now remember a subject in the world of gnats is the name of a channel.
So you can kind of thinking think of this as being like an event name or something very similar to that
inside the subject's file.
We going to export an e some old subjects.
Now if you've never worked with an E name before it's essentially somewhat similar to an object.
We're going to list out a couple of properties inside of here and we're going to then say if something
is of type subjects then it mean.
That means that it is one of the different properties that is listed out inside.
I'll show you an example of it right after we list out some different values inside of here.
It's inside of subjects we're gonna list all the possible subjects we expect to have.
Well we definitely expect to have a subject of ticket created and we might eventually have one of say
order updated as well.
We definitely don't yet.
But we might at the future.
So just to give an example we're going to add this in as a possible subject inside of the scene.
So going to say ticket created ticket colon created and then order was it updated.
Yes.
Order updated is order updated and I'll put a calm after that first century.
Like so.
So now just as a quick example of how we would use this enum we could make a function called Print subject
and we could say that in order to be an argument to this we'll take in some argument called subject
and it must be one of the members or one of the properties or types options whatever you want to call
it listed inside the CNN so to call print subject.
We cannot put in some random string we must put in one of the different options from that income so
to do so we would write out subjects dot ticket created like so and that works just fine.
Or alternatively we could do order updated as well.
OK so as time goes on and we start to add in more possible events to our application we're going to
come back to the subject's file and list them out inside of here.
This file alone is going to save us a ton of different typos.
We're going to list out the actual event name or the actual subject that we're going to use with the
Nats as a hard coded string inside of here and then inside of all of our code for example inside of
our listener that we just created rather than referring to this hardcoded string right here where we
could very easily make a typo like say ticket created instead of the intended ticket colon created we
will instead refer to say subjects dot ticket created and again that's gonna help us from making any
very simple straightforward typos.
So this already is a good improvement but we're not using it anywhere just yet.
Let's take a quick pause right here in the next video.
We're gonna start to come up with this structure that's going to allow typescript to understand that
there is a tight coupling of sorts between a ticket subject or this ticket created subject and this
event data type of idea title and price.