1 00:00:00,760 --> 00:00:05,860 In this video I want to go over two more pieces of syntax around interfaces so to get started. 2 00:00:05,890 --> 00:00:10,390 I'm gonna go down to the print vehicle function inside of here you'll notice that our implementation 3 00:00:10,630 --> 00:00:14,830 now only accesses one property on the vehicle argument. 4 00:00:14,860 --> 00:00:18,250 The only thing where we are referencing is these summary property. 5 00:00:18,250 --> 00:00:19,950 So I got a question for you. 6 00:00:20,020 --> 00:00:25,360 We are saying that the argument must be a vehicle and must satisfy this interface is the terminology 7 00:00:25,360 --> 00:00:26,670 we would use. 8 00:00:26,800 --> 00:00:34,480 And we've said earlier that the vehicle must have a name a year broken and the summary function. 9 00:00:34,480 --> 00:00:39,610 However our function down here is now only accessing the summary property. 10 00:00:39,610 --> 00:00:46,520 So here's the question is it important for us to say that a vehicle must have these properties available. 11 00:00:46,570 --> 00:00:48,360 I don't think it really is. 12 00:00:48,370 --> 00:00:51,130 So what happens if I delete those additional properties. 13 00:00:52,200 --> 00:00:52,990 I can delete them. 14 00:00:53,010 --> 00:00:53,860 They go away. 15 00:00:54,000 --> 00:00:58,050 And now you'll notice I do not see any errors inside my file. 16 00:00:58,080 --> 00:00:59,400 Why is that. 17 00:00:59,400 --> 00:01:05,100 Well when I call print vehicle right here we are saying that in order to be passed into this function 18 00:01:05,340 --> 00:01:08,910 you must satisfy the vehicle interface. 19 00:01:08,910 --> 00:01:13,920 So we pass an old Civic and typescript behind the scenes does a quick check for us it says. 20 00:01:13,920 --> 00:01:19,830 Does this object right here satisfied the vehicle interface so typescript is gonna go back to the vehicle 21 00:01:19,830 --> 00:01:25,080 interface definition and it says in order to be considered to be a vehicle you must have a function 22 00:01:25,110 --> 00:01:27,780 called summary that returns a string. 23 00:01:27,810 --> 00:01:34,980 It then looks at the old Civic object and it says OK does this object have a summary property that is 24 00:01:34,980 --> 00:01:37,730 a function that returns a string. 25 00:01:37,740 --> 00:01:39,300 Yes it does. 26 00:01:39,300 --> 00:01:43,570 And that's the only question that gets asked. 27 00:01:43,650 --> 00:01:46,740 It doesn't matter if we have additional properties on old civic. 28 00:01:46,740 --> 00:01:51,900 Doesn't matter when typescript tries to decide if old Civic is a vehicle or not. 29 00:01:51,900 --> 00:01:56,420 It's not going to say Oh you've got these extra properties sorry you can't be a vehicle. 30 00:01:56,640 --> 00:02:00,770 It's only going to check to see if it has this list of properties right here. 31 00:02:00,930 --> 00:02:08,490 If it has that list awesome it is qualified to be a vehicle so the code we have here still works. 32 00:02:08,500 --> 00:02:13,670 But we've got a much more condensed interface definition. 33 00:02:13,860 --> 00:02:15,230 So here's the important thing here. 34 00:02:15,240 --> 00:02:20,610 Something I want to point out as like kind of step two of this old refactor now that our interface only 35 00:02:20,610 --> 00:02:23,110 has a single function definition in it. 36 00:02:23,130 --> 00:02:26,430 Do you think the name vehicle is really appropriate here. 37 00:02:26,640 --> 00:02:32,690 Like is calling an object that has a summary property that is a function that returns a string. 38 00:02:32,790 --> 00:02:35,910 Does it really seem appropriate to call that a vehicle. 39 00:02:35,910 --> 00:02:37,560 I would say maybe it doesn't. 40 00:02:37,560 --> 00:02:42,540 Maybe a more appropriate interface or a name for this interface would now be something like. 41 00:02:42,690 --> 00:02:46,140 I don't know reportable or something like that. 42 00:02:46,200 --> 00:02:51,870 So now we're saying that in order to be considered the type reportable you must have a function called 43 00:02:51,870 --> 00:02:54,680 summary that returns a string. 44 00:02:54,740 --> 00:02:59,800 And so now to fix up our file here down here inside of print vehicles there is no more vehicle type 45 00:02:59,800 --> 00:03:04,490 song and replace that with reportable now just be clear the word reportable. 46 00:03:04,550 --> 00:03:09,790 I'm kind of meaning to say something that is reportable has a summary function that returns a string. 47 00:03:10,160 --> 00:03:14,470 So this is like something that can be reported all right. 48 00:03:14,470 --> 00:03:18,730 So notice how our definition for the interface is now a lot shorter and a little bit more generic than 49 00:03:18,730 --> 00:03:24,920 it was before it's now back down inside the print vehicle interface or function out here I want to just 50 00:03:24,920 --> 00:03:26,770 point out one more thing here. 51 00:03:26,780 --> 00:03:29,780 Notice how we've got a function called Print vehicle. 52 00:03:29,780 --> 00:03:35,420 It takes an argument called vehicle and it refers to that function or that argument inside the function. 53 00:03:35,420 --> 00:03:37,590 But once again I got a question for you. 54 00:03:37,730 --> 00:03:43,480 Does anything about this function now really seem like it has anything to do with vehicles. 55 00:03:43,490 --> 00:03:45,400 I don't think it really does. 56 00:03:45,410 --> 00:03:50,540 The only thing that this functionally does is taken something that can be reported on and then console 57 00:03:50,540 --> 00:03:53,620 log a summary about that thing. 58 00:03:53,840 --> 00:03:58,670 So maybe we should update some of the terminology inside this function maybe instead of an argument 59 00:03:58,670 --> 00:04:05,430 called vehicle we call this something that's called like I don't know report I don't like item. 60 00:04:05,720 --> 00:04:11,360 Maybe that makes sense and then inside are rather than coins thing print vehicle. 61 00:04:11,370 --> 00:04:15,420 I want to call it maybe something a little bit more appropriate like print summary or something like 62 00:04:15,420 --> 00:04:16,120 that. 63 00:04:16,230 --> 00:04:21,070 I'm going to update the reference down here as well to be simply print Summary Ok. 64 00:04:21,100 --> 00:04:25,820 So all we've really done here is kind of pointed out how our interface no longer really has anything 65 00:04:25,820 --> 00:04:27,980 to do specifically with vehicles. 66 00:04:27,980 --> 00:04:32,690 And so I just decided to rename it into something a little bit more generic and I did the same thing 67 00:04:32,690 --> 00:04:36,180 with the function down here so let's take a quick pause right here. 68 00:04:36,190 --> 00:04:40,810 We're gonna come back the next video and we're going to discuss why this was a very significant refactor 69 00:04:41,110 --> 00:04:45,810 by adding in just a little bit more code so quick pause and I'll see you in just a minute.