1 00:00:00,000 --> 00:00:02,010 - The next two built-in types 2 00:00:02,010 --> 00:00:05,460 of return type and parameters both deal with functions. 3 00:00:05,460 --> 00:00:07,470 So to get started we just have a really simple function 4 00:00:07,470 --> 00:00:09,180 that is going to showcase all these different things. 5 00:00:09,180 --> 00:00:10,170 It's called check length, 6 00:00:10,170 --> 00:00:11,220 takes in an A property 7 00:00:11,220 --> 00:00:12,870 which is a string and a B property, 8 00:00:12,870 --> 00:00:13,770 which is a number. 9 00:00:13,770 --> 00:00:15,990 And if the length of the string is less than the number 10 00:00:15,990 --> 00:00:19,080 we pass in it's going to return true, otherwise false. 11 00:00:19,080 --> 00:00:19,913 And I'm just doing this 12 00:00:19,913 --> 00:00:21,210 so we have a bunch of different types we can work with. 13 00:00:21,210 --> 00:00:23,190 We have strings, we have numbers, and we have Booleans. 14 00:00:23,190 --> 00:00:24,420 So if we check the signature of this 15 00:00:24,420 --> 00:00:26,190 you can see the return type for it 16 00:00:26,190 --> 00:00:28,650 is set to a Boolean right here. 17 00:00:28,650 --> 00:00:30,390 Now, if we wanted to use the return type 18 00:00:30,390 --> 00:00:32,760 of this function inside of our type, for example 19 00:00:32,760 --> 00:00:37,760 I wanted to get a type which is return of length check. 20 00:00:38,100 --> 00:00:40,260 Well, how exactly would I go about getting this? 21 00:00:40,260 --> 00:00:42,090 I don't have any way right now to do that. 22 00:00:42,090 --> 00:00:45,540 But with the return type function, I do. 23 00:00:45,540 --> 00:00:47,490 Return type takes in a type 24 00:00:47,490 --> 00:00:49,140 and this type must be a function 25 00:00:49,140 --> 00:00:51,240 and it'll give you whatever that function returns 26 00:00:51,240 --> 00:00:52,740 which in our case is a Boolean. 27 00:00:52,740 --> 00:00:55,230 So you may think we just pass in check length. 28 00:00:55,230 --> 00:00:56,063 That's not gonna work 29 00:00:56,063 --> 00:00:58,920 because check length is a function and this expects a type. 30 00:00:58,920 --> 00:01:01,740 So we need to get the type of our check length function. 31 00:01:01,740 --> 00:01:03,120 And now if we cover over this 32 00:01:03,120 --> 00:01:05,760 you can see the return of it is set to a Boolean. 33 00:01:05,760 --> 00:01:06,900 The return type is really good 34 00:01:06,900 --> 00:01:10,050 if we just want to get whatever a function returns. 35 00:01:10,050 --> 00:01:11,940 If you have a function declared like this 36 00:01:11,940 --> 00:01:13,170 as like JavaScript code 37 00:01:13,170 --> 00:01:15,120 you need to make sure you get the type of it. 38 00:01:15,120 --> 00:01:17,760 Otherwise, if you have a type that is an actual function 39 00:01:17,760 --> 00:01:20,160 so we'll say type func equals 40 00:01:20,160 --> 00:01:21,930 and this is just going to return to us void. 41 00:01:21,930 --> 00:01:23,370 It's just a really simple function. 42 00:01:23,370 --> 00:01:24,900 I could just pass that in right there 43 00:01:24,900 --> 00:01:27,023 because it's just expecting a type. And if I hover over here 44 00:01:27,023 --> 00:01:29,520 you can see it gives me void as that return type. 45 00:01:29,520 --> 00:01:30,353 This is really useful 46 00:01:30,353 --> 00:01:32,040 if you need to make sure you take the value 47 00:01:32,040 --> 00:01:33,720 of a return type and use it somewhere else 48 00:01:33,720 --> 00:01:36,480 in your code as like a parameter or another type. 49 00:01:36,480 --> 00:01:38,220 Now another slightly less useful, 50 00:01:38,220 --> 00:01:40,950 but still useful built-in type is the parameter type. 51 00:01:40,950 --> 00:01:42,090 Let's say I wanted to figure out 52 00:01:42,090 --> 00:01:45,900 what the type of the parameter A or B is in this function. 53 00:01:45,900 --> 00:01:49,230 But what I can do is I can use the parameters type 54 00:01:49,230 --> 00:01:52,320 and this parameters type takes in our function. 55 00:01:52,320 --> 00:01:54,243 So we'll use check length in our case, 56 00:01:55,080 --> 00:01:57,390 we'll type of check length just like that. 57 00:01:57,390 --> 00:01:59,340 And now this is gonna be our params type. 58 00:01:59,340 --> 00:02:00,270 And if we hover over this 59 00:02:00,270 --> 00:02:01,920 you can see that it gives us an array. 60 00:02:01,920 --> 00:02:03,990 And the very first thing in here is a string. 61 00:02:03,990 --> 00:02:05,640 And the second thing is a number. 62 00:02:05,640 --> 00:02:07,470 So it's like a two pull essentially 63 00:02:07,470 --> 00:02:09,390 because it's saying we accept two parameters, 64 00:02:09,390 --> 00:02:11,460 the first one is a parameter A, which is a string. 65 00:02:11,460 --> 00:02:13,500 The second one is a parameter B, which is a number. 66 00:02:13,500 --> 00:02:15,450 So if I wanted the very first parameter 67 00:02:15,450 --> 00:02:17,430 I could do this and this should return to me a string. 68 00:02:17,430 --> 00:02:19,260 As you can see, this is set to a string. 69 00:02:19,260 --> 00:02:21,540 While if I wanted the second parameter, I'm over here 70 00:02:21,540 --> 00:02:23,730 you can see now this is set to a number. 71 00:02:23,730 --> 00:02:26,250 That's a really good way to essentially get a type based 72 00:02:26,250 --> 00:02:28,710 on the parameters of a specific function. 73 00:02:28,710 --> 00:02:30,660 And if the function has no parameters, for example 74 00:02:30,660 --> 00:02:33,090 this func right here has no parameters, 75 00:02:33,090 --> 00:02:34,500 you can see when I try to look at this 76 00:02:34,500 --> 00:02:35,760 it's just going to be an empty array 77 00:02:35,760 --> 00:02:36,593 'cause it's saying, 78 00:02:36,593 --> 00:02:38,910 "Hey, there are no parameters at all for this function." 79 00:02:38,910 --> 00:02:40,530 Now this is definitely less useful, 80 00:02:40,530 --> 00:02:42,360 but sometimes if you're working in a library 81 00:02:42,360 --> 00:02:44,580 where you need to pass along things to a function 82 00:02:44,580 --> 00:02:46,530 but they don't have good types being exposed, 83 00:02:46,530 --> 00:02:48,240 you may need to use this parameters type 84 00:02:48,240 --> 00:02:50,460 to figure out what the actual type of that parameter is 85 00:02:50,460 --> 00:02:52,260 so you can set that type inside of your code. 86 00:02:52,260 --> 00:02:54,540 But like I said, parameters is much less useful. 87 00:02:54,540 --> 00:02:55,560 But return type is something 88 00:02:55,560 --> 00:02:57,000 you're going to be using all the time 89 00:02:57,000 --> 00:02:58,470 because it's very common to type something 90 00:02:58,470 --> 00:03:00,330 based on the return of another function. 91 00:03:00,330 --> 00:03:03,000 And since everything in TypeScript is inferred by default 92 00:03:03,000 --> 00:03:04,380 this Boolean is already inferred. 93 00:03:04,380 --> 00:03:06,060 I don't need to manually set that anywhere. 94 00:03:06,060 --> 00:03:08,310 So it's really good to be able to continue to infer it 95 00:03:08,310 --> 00:03:10,890 by getting the return type of that specific function 96 00:03:10,890 --> 00:03:13,500 without having to manually say, "Okay, you know what? 97 00:03:13,500 --> 00:03:14,850 This here is a Boolean, 98 00:03:14,850 --> 00:03:17,550 and then down here, this is also going to be a Boolean." 99 00:03:17,550 --> 00:03:18,540 I don't have to worry about that 100 00:03:18,540 --> 00:03:20,280 if I have all these different built-in types 101 00:03:20,280 --> 00:03:22,500 that could just constantly build on the inferring, 102 00:03:22,500 --> 00:03:23,940 which means I write less TypeScript, 103 00:03:23,940 --> 00:03:26,090 but you still get all of the same benefits.