1
00:00:01,040 --> 00:00:05,120
Now that we've seen a couple of examples of raise, I want to walk through some kind of corner cases

2
00:00:05,120 --> 00:00:05,620
around them.

3
00:00:05,960 --> 00:00:08,990
So essentially I want to discuss why we care about these rates.

4
00:00:08,990 --> 00:00:14,030
Like what's so big a deal about a raise in typescript that we're making this into a separate topic?

5
00:00:14,480 --> 00:00:15,230
Well, here's why.

6
00:00:15,770 --> 00:00:20,330
When we work with the raise, typescript, we got a couple of different advantages and a couple of different

7
00:00:20,330 --> 00:00:21,450
downsides as well.

8
00:00:22,100 --> 00:00:27,680
So first off, TypeScript can help us when we make a type turei by helping us do type inference whenever

9
00:00:27,680 --> 00:00:30,320
we are pulling a value out of an array.

10
00:00:30,770 --> 00:00:32,090
Let's see a quick example of that.

11
00:00:32,900 --> 00:00:34,490
Zoback back over inside my code editor.

12
00:00:34,490 --> 00:00:38,810
I'm going to put a little comment down right here and I'll say help with inference.

13
00:00:40,390 --> 00:00:47,020
When extracting values, so in other words, if we look at our carmaker's array right here, we know

14
00:00:47,020 --> 00:00:51,430
that it is an array of strings, typescript knows that as well through type inference.

15
00:00:52,150 --> 00:00:57,220
So that means that if we ever pull an element out of this array, either by using, say, the pop method

16
00:00:57,250 --> 00:01:03,820
or by direct index access, typescript will know that we are going to be pulling out a string and help

17
00:01:03,820 --> 00:01:07,870
us with type inference with the new variable that we're assigning this this value to.

18
00:01:08,350 --> 00:01:10,210
So let's take a look at an example of that.

19
00:01:10,210 --> 00:01:13,900
Let's say that I'm making a new variable right here and I'm going to call it something like car.

20
00:01:15,280 --> 00:01:22,000
If I then take car makers and do an index access to it, I can now hover over car and you'll see that

21
00:01:22,000 --> 00:01:24,010
type inference has come into play in.

22
00:01:24,010 --> 00:01:26,050
TypeScript knows that this variable is going to be a string.

23
00:01:26,650 --> 00:01:32,020
It knows this because it knows that carmakers is an array of strings and we just pulled one value out

24
00:01:32,020 --> 00:01:32,320
of it.

25
00:01:33,870 --> 00:01:39,780
Likewise, this also works if we try a pop method, so let's say we've got my car, would you like car

26
00:01:39,800 --> 00:01:46,710
makers pop like so to remove the last element in the array, once again, TypeScript knows that carmakers

27
00:01:46,710 --> 00:01:47,780
has an array of strings.

28
00:01:48,060 --> 00:01:53,700
So when we call Pop, it's going to return an instance of a string so we can once again hover over my

29
00:01:53,700 --> 00:01:54,690
car and we see up.

30
00:01:54,900 --> 00:01:57,930
TypeScript has correctly inferred that my car is going to be a string.

31
00:01:59,800 --> 00:02:00,130
All right.

32
00:02:00,160 --> 00:02:05,890
That's one benefit of making use of a raise and typescript secondly, typescript can help us prevent

33
00:02:06,180 --> 00:02:11,440
or help prevent us from adding in incompatible values into an array if we have correctly type the array.

34
00:02:12,040 --> 00:02:17,890
So, for example, back over here once again, we can say prevent in compatable.

35
00:02:19,710 --> 00:02:20,370
Values.

36
00:02:20,790 --> 00:02:26,250
So once again, if we take our carmakers away right here, that only contains strings and we tried to

37
00:02:26,250 --> 00:02:31,650
add in some element that is not a string like let's say a number will very quickly see an error message.

38
00:02:31,650 --> 00:02:36,450
TypeScript is telling us here, hey, you're trying to put in a number to a collection that is only

39
00:02:36,450 --> 00:02:37,710
supposed to contain strings.

40
00:02:38,290 --> 00:02:41,970
So that's going to help us from putting in dissimilar elements into one single array.

41
00:02:42,850 --> 00:02:47,620
And once again, if we want to have multiple different types in an array, we definitely can do that,

42
00:02:47,710 --> 00:02:49,960
but we have to use some special syntax to do so.

43
00:02:50,150 --> 00:02:52,260
We'll take a look at an example of that in just a moment.

44
00:02:53,580 --> 00:02:59,550
All right, third, any time we declare an array, we get a lot of help with built-In functions like

45
00:02:59,550 --> 00:03:04,440
map for each reduce and so on, whenever we are iterating over a collection of elements.

46
00:03:05,160 --> 00:03:07,710
So, for example, if we look back over once again.

47
00:03:09,650 --> 00:03:17,180
We get help with map or for each and reduce, so, for example, if we do car makers map, we can add

48
00:03:17,180 --> 00:03:20,540
in a function rakia that will be called with every element in the array.

49
00:03:21,170 --> 00:03:23,990
So I'm going to receive each car as a string.

50
00:03:24,830 --> 00:03:27,690
And from this I'm going to return, say, a string as well.

51
00:03:27,710 --> 00:03:33,290
We'll just do a straight pass through here, so I'll take the car and return it immediately.

52
00:03:34,350 --> 00:03:39,000
So TypeScript is going to make sure that the value that we are putting into this function right here

53
00:03:39,000 --> 00:03:40,200
will be a string.

54
00:03:41,460 --> 00:03:45,840
And so because of that, because TypeScript knows that Corey is going to be a string, we get access

55
00:03:45,840 --> 00:03:49,260
to a lot of different autocomplete on the variable that comes in here.

56
00:03:49,560 --> 00:03:54,450
So, for example, if I put in a dot right after car, I can then see the autocomplete appear with all

57
00:03:54,450 --> 00:03:56,310
the different methods that belong to strings.

58
00:03:56,340 --> 00:03:58,950
So, for example, I can do a two upper.

59
00:03:59,880 --> 00:04:01,050
Case like so.

60
00:04:02,710 --> 00:04:07,480
So any time we start working with bat for each reduce or any similar, Helper's typescript is going

61
00:04:07,480 --> 00:04:12,280
to give us autocomplete on the variable being passed into this function right here, which is a nice

62
00:04:12,280 --> 00:04:12,940
little benefit.

63
00:04:14,250 --> 00:04:18,720
All right, now, the last item we're going to talk about right here is that we can, in fact, put

64
00:04:18,720 --> 00:04:20,700
different types of elements inside of an array.

65
00:04:21,730 --> 00:04:24,970
Let's take a quick pause, though, because there's a couple of different corner cases around this and

66
00:04:24,970 --> 00:04:27,730
some interesting syntax we need to understand at the same time.

67
00:04:27,940 --> 00:04:30,040
It's a quick pause and I'll see you in just a minute.

