1 00:00:01,110 --> 00:00:05,340 In this video we're going to start to add in several pieces of code to make sure that typescript can 2 00:00:05,340 --> 00:00:10,920 make sure that whenever we create a listener we've got some matching subject and data type argument 3 00:00:10,920 --> 00:00:13,610 provided inside the on message function. 4 00:00:13,710 --> 00:00:14,800 So let's get to it. 5 00:00:14,820 --> 00:00:19,320 The first thing we're going to do inside of our events directory we're going to create a new file and 6 00:00:19,320 --> 00:00:23,160 inside there we're going to put together something that's going to list out all the different valid 7 00:00:23,220 --> 00:00:26,010 subject names that we ever expect to receive. 8 00:00:26,790 --> 00:00:31,770 So inside my events directory I'm going to make a new file called simply subjects dot. 9 00:00:31,810 --> 00:00:32,910 Yes. 10 00:00:32,910 --> 00:00:36,830 Now remember a subject in the world of gnats is the name of a channel. 11 00:00:36,870 --> 00:00:41,010 So you can kind of thinking think of this as being like an event name or something very similar to that 12 00:00:42,060 --> 00:00:43,310 inside the subject's file. 13 00:00:43,340 --> 00:00:47,880 We going to export an e some old subjects. 14 00:00:47,940 --> 00:00:52,720 Now if you've never worked with an E name before it's essentially somewhat similar to an object. 15 00:00:52,860 --> 00:00:57,570 We're going to list out a couple of properties inside of here and we're going to then say if something 16 00:00:57,570 --> 00:00:59,660 is of type subjects then it mean. 17 00:00:59,670 --> 00:01:03,150 That means that it is one of the different properties that is listed out inside. 18 00:01:03,150 --> 00:01:06,700 I'll show you an example of it right after we list out some different values inside of here. 19 00:01:06,860 --> 00:01:10,830 It's inside of subjects we're gonna list all the possible subjects we expect to have. 20 00:01:10,860 --> 00:01:16,630 Well we definitely expect to have a subject of ticket created and we might eventually have one of say 21 00:01:16,660 --> 00:01:18,070 order updated as well. 22 00:01:18,070 --> 00:01:18,890 We definitely don't yet. 23 00:01:18,910 --> 00:01:20,220 But we might at the future. 24 00:01:20,260 --> 00:01:24,530 So just to give an example we're going to add this in as a possible subject inside of the scene. 25 00:01:25,110 --> 00:01:33,570 So going to say ticket created ticket colon created and then order was it updated. 26 00:01:33,570 --> 00:01:33,950 Yes. 27 00:01:33,960 --> 00:01:43,290 Order updated is order updated and I'll put a calm after that first century. 28 00:01:43,300 --> 00:01:44,770 Like so. 29 00:01:44,840 --> 00:01:51,440 So now just as a quick example of how we would use this enum we could make a function called Print subject 30 00:01:52,540 --> 00:01:57,980 and we could say that in order to be an argument to this we'll take in some argument called subject 31 00:01:57,980 --> 00:02:04,040 and it must be one of the members or one of the properties or types options whatever you want to call 32 00:02:04,040 --> 00:02:08,740 it listed inside the CNN so to call print subject. 33 00:02:08,740 --> 00:02:13,900 We cannot put in some random string we must put in one of the different options from that income so 34 00:02:13,900 --> 00:02:20,920 to do so we would write out subjects dot ticket created like so and that works just fine. 35 00:02:21,090 --> 00:02:25,690 Or alternatively we could do order updated as well. 36 00:02:25,750 --> 00:02:30,110 OK so as time goes on and we start to add in more possible events to our application we're going to 37 00:02:30,110 --> 00:02:33,580 come back to the subject's file and list them out inside of here. 38 00:02:33,650 --> 00:02:37,430 This file alone is going to save us a ton of different typos. 39 00:02:37,430 --> 00:02:41,840 We're going to list out the actual event name or the actual subject that we're going to use with the 40 00:02:41,840 --> 00:02:46,940 Nats as a hard coded string inside of here and then inside of all of our code for example inside of 41 00:02:46,940 --> 00:02:51,860 our listener that we just created rather than referring to this hardcoded string right here where we 42 00:02:51,860 --> 00:02:59,270 could very easily make a typo like say ticket created instead of the intended ticket colon created we 43 00:02:59,270 --> 00:03:05,890 will instead refer to say subjects dot ticket created and again that's gonna help us from making any 44 00:03:05,950 --> 00:03:08,380 very simple straightforward typos. 45 00:03:08,440 --> 00:03:12,270 So this already is a good improvement but we're not using it anywhere just yet. 46 00:03:12,280 --> 00:03:14,530 Let's take a quick pause right here in the next video. 47 00:03:14,590 --> 00:03:19,150 We're gonna start to come up with this structure that's going to allow typescript to understand that 48 00:03:19,150 --> 00:03:25,330 there is a tight coupling of sorts between a ticket subject or this ticket created subject and this 49 00:03:25,330 --> 00:03:27,850 event data type of idea title and price.