1
00:00:00,710 --> 00:00:04,820
In the last video, we saw that we could remove all these different type annotations and we did not

2
00:00:04,820 --> 00:00:08,730
see any errors, in fact, if we then hovered over these variables.

3
00:00:08,750 --> 00:00:13,130
It's clear that TypeScript still understands what type of value we're going to assign to each given

4
00:00:13,130 --> 00:00:13,550
variable.

5
00:00:14,180 --> 00:00:17,870
So what you're seeing here is the type inference system in action.

6
00:00:18,320 --> 00:00:22,310
In this video, we're going to discuss how type inference works and then discuss when we're going to

7
00:00:22,310 --> 00:00:26,080
rely on type inference versus adding in an annotation manually.

8
00:00:26,330 --> 00:00:27,240
So let's get to it.

9
00:00:27,890 --> 00:00:28,190
All right.

10
00:00:28,190 --> 00:00:30,230
So first off, the basics of type inference.

11
00:00:30,830 --> 00:00:35,210
Anytime you and I make a new variable inside of our application, we're actually undergoing two separate

12
00:00:35,210 --> 00:00:35,720
steps.

13
00:00:36,290 --> 00:00:41,060
So on the left hand side of this equals sign where we say const color, we're doing something called

14
00:00:41,060 --> 00:00:42,250
variable declaration.

15
00:00:42,710 --> 00:00:46,370
This is where we are telling our program that we're going to have a new variable inside of our application

16
00:00:46,370 --> 00:00:47,210
with the given name.

17
00:00:48,130 --> 00:00:53,080
And then in the second step of this entire process, on the right hand side, the equals sign, we are

18
00:00:53,080 --> 00:00:55,600
attempting to assign a value to that variable.

19
00:00:56,350 --> 00:01:01,240
Assigning a value to a variable for the very first time is referred to as variable initialization.

20
00:01:01,870 --> 00:01:07,900
So this entire line of code right here, two separate steps, first variable declaration and then initialization.

21
00:01:08,920 --> 00:01:14,920
Here's the rule around type inference, if we do our declaration and initialization on the same line

22
00:01:14,920 --> 00:01:20,350
or essentially in one single expression, then TypeScript is going to figure out the type of value that

23
00:01:20,380 --> 00:01:22,270
we're going to assign to color for us.

24
00:01:23,050 --> 00:01:27,160
So in this case, TypeScript would look at the variable that were the value that we're usually using

25
00:01:27,160 --> 00:01:28,090
for initialization.

26
00:01:28,510 --> 00:01:33,670
It would see that this is a string and it would say, OK, color must be a variable that's going to

27
00:01:33,670 --> 00:01:35,680
refer to a value of type string.

28
00:01:36,430 --> 00:01:42,190
And that's how we get this kind of redone type right here where we can delete the annotation.

29
00:01:42,190 --> 00:01:44,430
And TypeScript still sees what's going on for us.

30
00:01:45,190 --> 00:01:50,500
If I moved this assignment or this initialization onto a separate line, then it's not going to work

31
00:01:50,500 --> 00:01:50,880
anymore.

32
00:01:51,280 --> 00:01:56,290
So if I say Apple's equals five on the next line down by hover over Apple's, you'll notice that it

33
00:01:56,290 --> 00:01:58,140
has a different annotation on here.

34
00:01:58,150 --> 00:02:02,470
It says any in this case, we're going to discuss with any means in just a moment.

35
00:02:02,590 --> 00:02:06,730
But right now, I think you can agree with me that TypeScript can no longer figure out what type of

36
00:02:06,730 --> 00:02:08,289
value we're going to assign to apples.

37
00:02:08,889 --> 00:02:14,080
So, again, the rule of thumb here is if you're doing the initialization and declaration on the same

38
00:02:14,080 --> 00:02:16,900
line, TypeScript will use type inference for us.

39
00:02:18,130 --> 00:02:22,420
OK, so that's the kind of easy part of type in France would do everything on one line, everything's

40
00:02:22,420 --> 00:02:23,290
going to work just fine.

41
00:02:23,860 --> 00:02:26,140
So now here's the really obvious question.

42
00:02:26,620 --> 00:02:30,040
When are we going to add in these type annotations?

43
00:02:30,340 --> 00:02:31,450
When are we going to type them out?

44
00:02:31,810 --> 00:02:36,100
If we're saying that TypeScript can figure out these different types for us, why would we go through

45
00:02:36,100 --> 00:02:38,550
the process of writing out the invitations manually?

46
00:02:38,800 --> 00:02:39,850
And that's a great question.

47
00:02:40,450 --> 00:02:45,580
Let's take a look at a diagram to understand why we might want to use type inference over annotation

48
00:02:45,730 --> 00:02:47,110
or the opposite.

49
00:02:48,390 --> 00:02:52,500
OK, so in that file we just put together, we've got a lot of examples of type annotations.

50
00:02:52,510 --> 00:02:57,480
So remember, that's where you and I, the developers are going to tell TypeScript the type that a value

51
00:02:57,480 --> 00:02:59,250
or a variable is going to refer to.

52
00:03:00,800 --> 00:03:04,230
Now, type inference, that's where TypeScript is going to guess everything for us.

53
00:03:04,880 --> 00:03:10,550
So in general, we're going to rely on type inference Ollis all the time, wherever we can.

54
00:03:10,550 --> 00:03:12,560
We will rely on type inference.

55
00:03:13,160 --> 00:03:17,960
That means that whenever you and I have a variable that we are declaring and then initializing on the

56
00:03:17,960 --> 00:03:22,070
same line, you and I in this course are not going to add in type annotations.

57
00:03:22,490 --> 00:03:24,920
So we're going to have a lot of code that looks like this right here.

58
00:03:25,160 --> 00:03:30,050
And we will have much more rarely have code that looks like this down here or anything else that you

59
00:03:30,050 --> 00:03:31,010
see inside this file.

60
00:03:31,820 --> 00:03:35,510
So we are going to rely upon type inference as much as we possibly can.

61
00:03:36,170 --> 00:03:40,400
But there are three scenarios where we will rely on type annotations.

62
00:03:41,150 --> 00:03:42,410
So let's take a quick break right here.

63
00:03:42,410 --> 00:03:46,190
And we're going to go through these three different examples of where we are going to add in these type

64
00:03:46,190 --> 00:03:48,170
annotations to help type script out.

65
00:03:48,440 --> 00:03:50,420
So quick pause and I'll see you in just a minute.

