1 00:00:00,120 --> 00:00:02,010 - So to get started talking about the never type 2 00:00:02,010 --> 00:00:03,990 I'm actually gonna be using the exact code 3 00:00:03,990 --> 00:00:04,830 from the last video 4 00:00:04,830 --> 00:00:06,240 because it ties in perfectly 5 00:00:06,240 --> 00:00:08,910 with what the never type is meant to be used for 6 00:00:08,910 --> 00:00:10,590 and the never type in TypeScript, 7 00:00:10,590 --> 00:00:11,670 it's just a type that means 8 00:00:11,670 --> 00:00:14,340 that it is never possible for it to be anything. 9 00:00:14,340 --> 00:00:15,750 Essentially, the never type is saying 10 00:00:15,750 --> 00:00:16,950 that you should never be able 11 00:00:16,950 --> 00:00:18,600 to get to this point in the code 12 00:00:18,600 --> 00:00:20,670 and this variable should never even exist 13 00:00:20,670 --> 00:00:23,340 'cause it never has any type to it at all. 14 00:00:23,340 --> 00:00:25,290 The perfect example is a switch statement 15 00:00:25,290 --> 00:00:26,690 where you have essentially three values here. 16 00:00:26,690 --> 00:00:27,870 It could be any number of values, 17 00:00:27,870 --> 00:00:28,800 but we have three values 18 00:00:28,800 --> 00:00:31,290 and we're accounting for all those different values. 19 00:00:31,290 --> 00:00:32,430 So when I come into here 20 00:00:32,430 --> 00:00:34,440 and I try to create a default case 21 00:00:34,440 --> 00:00:36,000 and I try to counsel log, 22 00:00:36,000 --> 00:00:37,320 just like I've been doing before, 23 00:00:37,320 --> 00:00:39,030 what my priority is 24 00:00:39,030 --> 00:00:42,330 you'll notice here that the priority has a type of never. 25 00:00:42,330 --> 00:00:43,290 And the reason for that 26 00:00:43,290 --> 00:00:45,600 is because we've checked for high, normal and low 27 00:00:45,600 --> 00:00:48,840 which are the only three options that our type can be. 28 00:00:48,840 --> 00:00:50,610 So we know if we've checked for all three 29 00:00:50,610 --> 00:00:51,750 of these different types, 30 00:00:51,750 --> 00:00:54,330 that it is impossible for it to be anything else. 31 00:00:54,330 --> 00:00:55,470 So it's impossible for it 32 00:00:55,470 --> 00:00:57,570 to reach this default value section. 33 00:00:57,570 --> 00:00:59,760 This type may seem useless to you, 34 00:00:59,760 --> 00:01:00,637 other than the fact that, 35 00:01:00,637 --> 00:01:02,550 "Hey, I know that this can never be anything", 36 00:01:02,550 --> 00:01:04,170 so when I try to do something on it, 37 00:01:04,170 --> 00:01:06,180 you'll notice I get no auto complete, no nothing 38 00:01:06,180 --> 00:01:07,440 and I'm always gonna get an error 39 00:01:07,440 --> 00:01:09,420 because the never type cannot be used 40 00:01:09,420 --> 00:01:11,040 with anything, obviously. 41 00:01:11,040 --> 00:01:12,720 This is actually slightly more useful 42 00:01:12,720 --> 00:01:13,830 than you may think. 43 00:01:13,830 --> 00:01:15,900 One thing that you can do with the never type 44 00:01:15,900 --> 00:01:17,490 is you can actually check to see 45 00:01:17,490 --> 00:01:19,080 if something is the never type 46 00:01:19,080 --> 00:01:21,030 to know if you are actually checking 47 00:01:21,030 --> 00:01:22,710 all your possible options. 48 00:01:22,710 --> 00:01:23,670 What we can do down here 49 00:01:23,670 --> 00:01:25,410 is we can just essentially create a variable 50 00:01:25,410 --> 00:01:27,690 to determine if we're exhaustively checking this. 51 00:01:27,690 --> 00:01:28,860 It doesn't matter what you call this 52 00:01:28,860 --> 00:01:31,080 we'll call this exhaustive check, 53 00:01:31,080 --> 00:01:32,010 just like that. 54 00:01:32,010 --> 00:01:35,010 And we're gonna say that the type of this is never 55 00:01:35,010 --> 00:01:36,120 and then we're gonna set that equal 56 00:01:36,120 --> 00:01:37,590 to our todo.priority. 57 00:01:37,590 --> 00:01:38,880 Essentially, we're just gonna set this 58 00:01:38,880 --> 00:01:40,440 to whatever the thing up here is 59 00:01:40,440 --> 00:01:41,880 that we're switching over. 60 00:01:41,880 --> 00:01:43,200 And then we can do whatever we want. 61 00:01:43,200 --> 00:01:44,033 It really doesn't matter. 62 00:01:44,033 --> 00:01:45,480 We can just return it or do whatever. 63 00:01:45,480 --> 00:01:47,010 It should never get to this point. 64 00:01:47,010 --> 00:01:48,450 So it doesn't really matter. 65 00:01:48,450 --> 00:01:50,400 And let me just make sure I spell this correctly, 66 00:01:50,400 --> 00:01:52,080 so it's a little bit better. 67 00:01:52,080 --> 00:01:53,040 There we go. 68 00:01:53,040 --> 00:01:54,480 So it should never ever get to this point. 69 00:01:54,480 --> 00:01:56,370 And right now all of our code is working fine 70 00:01:56,370 --> 00:01:58,410 but what happens if we add a brand new priority? 71 00:01:58,410 --> 00:02:01,650 Let's come in here and we'll say like super low priority. 72 00:02:01,650 --> 00:02:02,730 Now when I save and come down here 73 00:02:02,730 --> 00:02:04,200 you're gonna notice I get an error 74 00:02:04,200 --> 00:02:05,400 and it's essentially saying the type 75 00:02:05,400 --> 00:02:07,860 of string is not assignable to the type of never. 76 00:02:07,860 --> 00:02:09,540 And that's 'cause this priority here 77 00:02:09,540 --> 00:02:10,830 could still be super low. 78 00:02:10,830 --> 00:02:12,780 We've checked for high, normal and low 79 00:02:12,780 --> 00:02:14,640 but we haven't checked for super low. 80 00:02:14,640 --> 00:02:16,650 This is gonna throw a TypeScript error for me 81 00:02:16,650 --> 00:02:17,483 letting me know, 82 00:02:17,483 --> 00:02:19,290 "Hey, you haven't checked for this thing yet." 83 00:02:19,290 --> 00:02:22,650 This is one use case where the never type is really useful. 84 00:02:22,650 --> 00:02:26,700 And what I can do is add in my case for super low, 85 00:02:26,700 --> 00:02:28,020 break out of it, do whatever we want. 86 00:02:28,020 --> 00:02:29,760 And now you can see that this is working again. 87 00:02:29,760 --> 00:02:31,290 So if you want to create a switch statement 88 00:02:31,290 --> 00:02:34,680 that requires you to check for every single possible option 89 00:02:34,680 --> 00:02:35,820 adding in some simple code 90 00:02:35,820 --> 00:02:37,230 like this where you just have a variable 91 00:02:37,230 --> 00:02:38,220 it doesn't matter what the name is, 92 00:02:38,220 --> 00:02:39,930 I'll just change it to A for simplicity, 93 00:02:39,930 --> 00:02:41,940 that you manually set to the type of never 94 00:02:41,940 --> 00:02:43,200 and you assign it to whatever 95 00:02:43,200 --> 00:02:45,240 the thing inside of your switch statement is 96 00:02:45,240 --> 00:02:46,230 what you're checking. 97 00:02:46,230 --> 00:02:47,730 That's gonna make sure you always check 98 00:02:47,730 --> 00:02:49,170 for every single possibility, 99 00:02:49,170 --> 00:02:51,620 which is really useful for union types like this.