1
00:00:00,840 --> 00:00:04,740
In this video, we're going to look at the final case in which we will make use of type annotations,

2
00:00:04,900 --> 00:00:09,840
so this will be whenever we have a variable where it's type cannot be reasonably inferred, even if

3
00:00:09,840 --> 00:00:13,150
the declaration and initialization are on the same line.

4
00:00:13,920 --> 00:00:16,800
This is really going to be a very big court case right here.

5
00:00:16,980 --> 00:00:20,550
Nonetheless, I just want you to understand when we want to use type annotations.

6
00:00:20,790 --> 00:00:22,770
So let's try to get through this example really quickly.

7
00:00:23,340 --> 00:00:24,690
I'm going to flip back over to my editor.

8
00:00:24,990 --> 00:00:28,200
And at the very bottom, once again, I'm going to add in a new section down here.

9
00:00:28,200 --> 00:00:29,640
And I'll say this is the third case.

10
00:00:29,880 --> 00:00:37,020
This is when we have a variable who is type cannot be inferred correctly.

11
00:00:38,800 --> 00:00:43,600
For this example, we're going to create a array of numbers called numbers, I'm going to give it values

12
00:00:43,600 --> 00:00:46,320
like negative ten, negative one and 12.

13
00:00:47,170 --> 00:00:50,330
I'll then declare a variable called number above zero.

14
00:00:51,070 --> 00:00:51,940
So here's the idea.

15
00:00:52,180 --> 00:00:53,860
I want to iterate through the numbers array.

16
00:00:54,310 --> 00:00:59,200
If we find a number that is greater than zero, I want to assign it to number above zero.

17
00:00:59,920 --> 00:01:03,100
Otherwise, I want to assign false to number above zero.

18
00:01:03,910 --> 00:01:08,350
So that means that we're going to have two different types of values being assigned to this variable.

19
00:01:08,920 --> 00:01:11,950
Now, this is some kind of bad code or writing right here.

20
00:01:11,950 --> 00:01:14,110
I really don't recommend taking this approach in general.

21
00:01:14,380 --> 00:01:17,590
However, there are scenarios where you might need to do something like this.

22
00:01:18,070 --> 00:01:20,440
Doing it with a number and a boolean doesn't make a lot of sense.

23
00:01:20,590 --> 00:01:25,810
But there might be other cases like maybe you're trying to find a user's favorite piece of media and

24
00:01:25,810 --> 00:01:32,260
maybe possible choices for media are like a blog post or an image or a book or a movie or something

25
00:01:32,260 --> 00:01:32,740
like that.

26
00:01:33,260 --> 00:01:38,920
So if we had something like, you know, users favorite media, it would possibly make sense to assign

27
00:01:38,920 --> 00:01:41,170
some different type of values to this variable.

28
00:01:42,170 --> 00:01:44,030
So this is kind of where the scenario is going to come up.

29
00:01:44,780 --> 00:01:48,580
All right, so we're going to say that by default, number above zero is going to be false.

30
00:01:48,860 --> 00:01:54,230
And then if we ever find something above zero inside this array, we need to assign that number to number

31
00:01:54,230 --> 00:01:54,850
above zero.

32
00:01:54,890 --> 00:01:55,910
So let's write this code out.

33
00:01:56,490 --> 00:02:00,890
Let's say four let equals zero, i.e. less than numbers that length.

34
00:02:03,390 --> 00:02:11,280
Plus, plus, and then if numbers that eye is greater than zero, we'll say no above zero is going to

35
00:02:11,280 --> 00:02:12,210
be that number.

36
00:02:12,270 --> 00:02:13,560
So let's give me numbers at I.

37
00:02:14,090 --> 00:02:16,290
As soon as we put that in, we're going to see an error message.

38
00:02:16,290 --> 00:02:18,240
And I definitely would expect to see this.

39
00:02:19,020 --> 00:02:24,120
So type inference already kicked in right here on this line of code we assign false to number above

40
00:02:24,120 --> 00:02:24,570
zero.

41
00:02:24,690 --> 00:02:28,590
So TypeScript very correctly thinks that this is supposed to be of type boolean.

42
00:02:29,130 --> 00:02:32,910
Then on this line of code right here, we're trying to assign something that is definitely a number

43
00:02:33,120 --> 00:02:33,930
to that variable.

44
00:02:34,230 --> 00:02:38,940
So the error is that we're trying to assign a number to a variable that is supposed to reference a boolean.

45
00:02:40,290 --> 00:02:44,940
So this is one of the downsides of type, inference, type inference kicked in here, it did its job,

46
00:02:44,940 --> 00:02:48,720
but it didn't really understand the intent of our code or what we're trying to do.

47
00:02:50,020 --> 00:02:55,090
So because type in France is marking this variable, as always, being a boolean, our code won't work

48
00:02:55,090 --> 00:02:55,750
as is.

49
00:02:56,020 --> 00:03:01,030
So to fix this, we could add in a type annotation so we could add in something that says Kullen.

50
00:03:02,310 --> 00:03:03,480
And then Boolean.

51
00:03:04,440 --> 00:03:08,440
Or no, like so this character right here is the pipe.

52
00:03:08,460 --> 00:03:13,500
It is not an L. It's not an eye is a character right above the return key on your keyboard.

53
00:03:14,720 --> 00:03:20,240
You can picture this as being like an understatement, so this is saying that number above zero is going

54
00:03:20,240 --> 00:03:25,650
to be either a boolean value or it might be a number one of the two.

55
00:03:26,450 --> 00:03:31,640
So any time that we need to express kind of like an overstatement with our types, we will have to use

56
00:03:31,640 --> 00:03:33,350
a type annotation type.

57
00:03:33,350 --> 00:03:35,000
Inference won't work correctly.

58
00:03:36,690 --> 00:03:41,220
So by using this type annotation, TypeScript now understands that we can assign a value of type boolean

59
00:03:41,520 --> 00:03:46,800
or a value of type number to its variable, and if we mouseover that variable, it'll be reflected right

60
00:03:46,800 --> 00:03:47,330
there as well.

61
00:03:48,230 --> 00:03:48,560
All right.

62
00:03:48,630 --> 00:03:54,230
Like I said, this is kind of a bad code example, but we could kind of easily imagine some other scenarios

63
00:03:54,230 --> 00:03:58,430
where we might have to express a single variable that has possibly different types.

64
00:03:58,700 --> 00:04:03,320
And we definitely are going to add some code that looks like this in a future project in this course.

65
00:04:03,560 --> 00:04:09,290
So we'll see this kind of scenario of where we have to add in a type annotation, but with a more practical

66
00:04:09,290 --> 00:04:09,770
approach.

67
00:04:10,550 --> 00:04:10,880
All right.

68
00:04:10,880 --> 00:04:11,550
So that's pretty much it.

69
00:04:11,570 --> 00:04:15,950
So we now have taken a look at the three different cases where we definitely will have to use a type

70
00:04:15,950 --> 00:04:16,550
annotation.

71
00:04:17,029 --> 00:04:18,220
So let's now take a quick pause.

72
00:04:18,230 --> 00:04:23,660
We're going to move on in the next video and talk about type annotations and inference around functions

73
00:04:23,660 --> 00:04:25,060
and objects as well.

74
00:04:25,100 --> 00:04:25,760
Where's that diagram?

75
00:04:25,760 --> 00:04:27,330
Right here, functions and objects.

76
00:04:27,330 --> 00:04:31,280
And we're going to get through these other approaches really quickly because we just had to do some,

77
00:04:31,280 --> 00:04:34,100
like, really original work in this variable section.

78
00:04:34,400 --> 00:04:36,400
So quick pause and I'll see you in just a minute.

