1 00:00:00,150 --> 00:00:01,290 - Now in this course so far 2 00:00:01,290 --> 00:00:02,640 we've talked a little bit about 3 00:00:02,640 --> 00:00:04,410 when TypeScript just assumes a variable 4 00:00:04,410 --> 00:00:06,180 can be any type at all. 5 00:00:06,180 --> 00:00:08,250 For example, if I define the variable A, 6 00:00:08,250 --> 00:00:09,630 and I don't give it a default value 7 00:00:09,630 --> 00:00:11,760 and I don't give it any type information, 8 00:00:11,760 --> 00:00:13,350 it just assumes that A can be anything. 9 00:00:13,350 --> 00:00:15,240 So I could set it to a number here, 10 00:00:15,240 --> 00:00:16,290 I could set it to a string, 11 00:00:16,290 --> 00:00:17,280 it's going to work just fine 12 00:00:17,280 --> 00:00:18,900 'cause TypeScript says, "I don't know what this is. 13 00:00:18,900 --> 00:00:20,550 It could be literally anything." 14 00:00:20,550 --> 00:00:22,560 And TypeScript actually has a specific type for that 15 00:00:22,560 --> 00:00:23,850 and that is called the any type. 16 00:00:23,850 --> 00:00:24,960 As you can see inside of here, 17 00:00:24,960 --> 00:00:25,800 if I hover over this, 18 00:00:25,800 --> 00:00:27,870 this is inferred as the type any. 19 00:00:27,870 --> 00:00:29,970 And I could manually type this as any as well 20 00:00:29,970 --> 00:00:31,710 by writing out any like this. 21 00:00:31,710 --> 00:00:33,480 And that's going to mark this as an any variable. 22 00:00:33,480 --> 00:00:35,460 So even if I give it a value of four, for example, 23 00:00:35,460 --> 00:00:37,110 it still says that this can be anything. 24 00:00:37,110 --> 00:00:38,730 So it doesn't matter what I assign it to, 25 00:00:38,730 --> 00:00:39,990 whether it's an object, an array, 26 00:00:39,990 --> 00:00:41,430 a number, a string, it doesn't matter, 27 00:00:41,430 --> 00:00:43,560 TypeScript says this could be anything. 28 00:00:43,560 --> 00:00:44,760 Now the great thing about this 29 00:00:44,760 --> 00:00:46,140 is it means you can just write your code 30 00:00:46,140 --> 00:00:47,190 like normal JavaScript, 31 00:00:47,190 --> 00:00:50,340 'cause JavaScript, every variable is any type by default. 32 00:00:50,340 --> 00:00:52,080 But obviously the downside is you lose 33 00:00:52,080 --> 00:00:53,280 pretty much all of the benefits 34 00:00:53,280 --> 00:00:54,930 of TypeScript when you use any. 35 00:00:54,930 --> 00:00:56,880 For example, I no longer have any auto complete 36 00:00:56,880 --> 00:00:58,440 because it doesn't know what this is. 37 00:00:58,440 --> 00:01:00,000 It doesn't know is it a string, is it a number, 38 00:01:00,000 --> 00:01:01,170 is it a Boolean, is it an array 39 00:01:01,170 --> 00:01:02,670 is it an object, is it a date? 40 00:01:02,670 --> 00:01:03,503 It has no idea. 41 00:01:03,503 --> 00:01:05,040 So you can't get any auto complete 42 00:01:05,040 --> 00:01:07,350 and you really don't get any error protection as well 43 00:01:07,350 --> 00:01:09,510 because again, TypeScript doesn't know what this is. 44 00:01:09,510 --> 00:01:10,860 so it can't prevent errors, 45 00:01:10,860 --> 00:01:12,390 'cause it doesn't know what it's trying to prevent. 46 00:01:12,390 --> 00:01:14,820 So really any is a bad idea. 47 00:01:14,820 --> 00:01:16,860 And most often when you're writing out code 48 00:01:16,860 --> 00:01:18,690 if you're writing any like this, 49 00:01:18,690 --> 00:01:20,040 that's generally a code smell 50 00:01:20,040 --> 00:01:22,050 and something that you should not do in your code. 51 00:01:22,050 --> 00:01:24,060 You almost always want to remove anys 52 00:01:24,060 --> 00:01:26,490 anywhere that they show up inside of your code. 53 00:01:26,490 --> 00:01:27,390 Now for the most part, 54 00:01:27,390 --> 00:01:29,280 you probably won't run into problems 55 00:01:29,280 --> 00:01:30,690 with any showing up in your code, 56 00:01:30,690 --> 00:01:33,450 unless you obviously manually add them yourselves 57 00:01:33,450 --> 00:01:34,980 or you're creating a variable 58 00:01:34,980 --> 00:01:36,450 without actually giving it a value 59 00:01:36,450 --> 00:01:37,620 or you give it a default value 60 00:01:37,620 --> 00:01:39,390 of like null or undefined, 61 00:01:39,390 --> 00:01:40,223 which in that case 62 00:01:40,223 --> 00:01:43,440 it's always going to be a default value type of any here. 63 00:01:43,440 --> 00:01:45,180 But the other scenarios where you may run 64 00:01:45,180 --> 00:01:46,800 into any that you need to be aware of, 65 00:01:46,800 --> 00:01:48,930 is if you use json.parse, 66 00:01:48,930 --> 00:01:50,280 you pass at some string. 67 00:01:50,280 --> 00:01:52,950 The actual return of json.parse is going to be any, 68 00:01:52,950 --> 00:01:54,960 for example if I say const A is equal to that 69 00:01:54,960 --> 00:01:55,890 and I hover over this, 70 00:01:55,890 --> 00:01:57,210 you can see it has a type of any. 71 00:01:57,210 --> 00:01:58,350 And that's just because TypeScript 72 00:01:58,350 --> 00:01:59,580 doesn't know what the object 73 00:01:59,580 --> 00:02:01,860 or value being returned from json.parse is. 74 00:02:01,860 --> 00:02:03,960 It could literally be anything at all. 75 00:02:03,960 --> 00:02:05,250 So it just needs to be able to account 76 00:02:05,250 --> 00:02:07,080 for all of those different things. 77 00:02:07,080 --> 00:02:09,480 Same thing if you make a fetch request, 78 00:02:09,480 --> 00:02:12,270 if I make a fetch request to some URL 79 00:02:12,270 --> 00:02:13,680 and then I want to get my data back, 80 00:02:13,680 --> 00:02:15,633 so I say res.json, 81 00:02:17,310 --> 00:02:19,773 now I have my data here that I can use. 82 00:02:21,810 --> 00:02:22,893 So we'll get data, 83 00:02:24,330 --> 00:02:26,850 oops, make sure I clean up my code a little bit. 84 00:02:26,850 --> 00:02:27,683 There we go. 85 00:02:27,683 --> 00:02:28,890 And if I go over this data, 86 00:02:28,890 --> 00:02:31,320 you can see that this is again a type of any, 87 00:02:31,320 --> 00:02:32,670 because it doesn't know what the data 88 00:02:32,670 --> 00:02:34,410 being returned from your API is. 89 00:02:34,410 --> 00:02:35,310 It could be JSON, 90 00:02:35,310 --> 00:02:36,600 it could be something entirely different. 91 00:02:36,600 --> 00:02:37,500 It has no idea. 92 00:02:37,500 --> 00:02:38,850 It could be unknown, undefined, 93 00:02:38,850 --> 00:02:41,790 doesn't know, so it just defaults the type two any. 94 00:02:41,790 --> 00:02:42,870 So that's something that's really important 95 00:02:42,870 --> 00:02:43,890 to make sure you understand 96 00:02:43,890 --> 00:02:46,620 because you may be using fetch inside your code base. 97 00:02:46,620 --> 00:02:48,120 You may have this data variable that you start 98 00:02:48,120 --> 00:02:49,950 accessing different properties on. 99 00:02:49,950 --> 00:02:51,270 And before you know it you realize 100 00:02:51,270 --> 00:02:52,103 that you're not getting 101 00:02:52,103 --> 00:02:53,160 the error protection that you need, 102 00:02:53,160 --> 00:02:54,990 'cause this is inferred as a type of any, 103 00:02:54,990 --> 00:02:56,340 which pretty much when you use any, 104 00:02:56,340 --> 00:02:57,570 it just shuts down TypeScript 105 00:02:57,570 --> 00:02:59,430 for anything that uses that variable 106 00:02:59,430 --> 00:03:01,230 or piece of code that you've typed as any 107 00:03:01,230 --> 00:03:03,210 because it just doesn't know what to do. 108 00:03:03,210 --> 00:03:05,880 Overall, my advice is to never use any, 109 00:03:05,880 --> 00:03:08,250 unless you're in the process of maybe converting 110 00:03:08,250 --> 00:03:10,800 a JavaScript project over to a TypeScript project, 111 00:03:10,800 --> 00:03:12,930 which I'll cover much later in this course. 112 00:03:12,930 --> 00:03:13,763 But pretty much 113 00:03:13,763 --> 00:03:15,300 if you're writing TypeScript code from scratch, 114 00:03:15,300 --> 00:03:16,710 there's almost no use case 115 00:03:16,710 --> 00:03:18,030 that you would want to use any in 116 00:03:18,030 --> 00:03:20,190 because it just essentially shuts down using TypeScript 117 00:03:20,190 --> 00:03:22,840 and gets rid of all the benefits of using TypeScript.