1
00:00:00,820 --> 00:00:04,860
Whenever you and I write typescript code, we're going to rely upon type inference quite heavily.

2
00:00:05,140 --> 00:00:08,920
This is where TypeScript is going to figure out the different types that are flowing around our application

3
00:00:08,920 --> 00:00:09,650
for us.

4
00:00:10,180 --> 00:00:13,690
However, there are three different scenarios that we're going to look at, where we're going to have

5
00:00:13,690 --> 00:00:15,730
to add in type annotations manually.

6
00:00:15,730 --> 00:00:19,630
Instead, remember, these type annotations are going to help TypeScript along.

7
00:00:20,350 --> 00:00:24,610
The first example we're going to look at is whenever we have a function that returns the any type and

8
00:00:24,610 --> 00:00:28,140
we need to clarify it and we haven't really discussed the any type just yet.

9
00:00:28,240 --> 00:00:32,140
So we're going to write out first just a little bit of code, then we'll review what the any type really

10
00:00:32,140 --> 00:00:36,700
means and then we'll get a better idea of why we need to use a type annotation in this case.

11
00:00:36,940 --> 00:00:37,810
So let's get to it.

12
00:00:38,400 --> 00:00:42,640
I'm going to flip back over to my code editor and I'm going to go down to the very bottom of the file

13
00:00:43,180 --> 00:00:43,600
down here.

14
00:00:43,600 --> 00:00:47,920
We're going to add in our three different examples on type annotations so I can put in a little comment

15
00:00:47,920 --> 00:00:51,190
that says, when do you use annotations?

16
00:00:52,140 --> 00:00:54,630
So case number one is when we have a function.

17
00:00:56,150 --> 00:00:57,260
That returns.

18
00:00:58,590 --> 00:01:00,030
The any type.

19
00:01:01,060 --> 00:01:05,650
OK, so for this example, I want to define a little snippet of Jason as a string.

20
00:01:05,860 --> 00:01:08,040
No, just going to try to pass that, Jason.

21
00:01:08,770 --> 00:01:14,650
So I'm going to define a new variable called lowercase Jason, and I'm going to assign it a JSON string.

22
00:01:14,920 --> 00:01:15,940
So I'll put down a string.

23
00:01:15,940 --> 00:01:18,670
And then inside of here, we're going to write out just a little bit of JSON.

24
00:01:19,490 --> 00:01:20,680
So let's say curly brace.

25
00:01:21,070 --> 00:01:24,610
I'll put down X and double quotes, a colon and then 10.

26
00:01:25,740 --> 00:01:31,500
Then why, in double quotes, a colon and 20, and then I'll close off the curly brace like so.

27
00:01:32,770 --> 00:01:38,140
So now I could use that string with the JSON, parse function and turn the string into an actual object,

28
00:01:39,020 --> 00:01:41,890
so I'm going to create a new variable called How about coordinates?

29
00:01:42,900 --> 00:01:49,350
In a sign that the result of Jason that pass with that Jason variable, so in theory, if we now ran

30
00:01:49,350 --> 00:01:58,020
this code and console logged out coordinates, we would see something like X is 10 and Y is 20 like

31
00:01:58,020 --> 00:01:58,320
so.

32
00:02:00,150 --> 00:02:04,770
Now, there's just one little gotcha inside of here, if you mouse over it coordinates, you'll notice

33
00:02:04,770 --> 00:02:06,750
that it has a type connotation of any.

34
00:02:06,840 --> 00:02:11,039
So this means that TypeScript thinks that coordinates is of type any.

35
00:02:12,110 --> 00:02:16,790
In addition, if we mouseover pass right here, you'll notice a lot of nasty syntax up here, but at

36
00:02:16,790 --> 00:02:22,070
the very end of it is something that says Colon, any that means that the JSON parts function returns

37
00:02:22,070 --> 00:02:23,090
the any type.

38
00:02:24,070 --> 00:02:26,340
So clearly, this anything is something pretty important.

39
00:02:26,530 --> 00:02:32,230
Let's have a quick pause, a little aside here and discuss what the any type means and then we'll understand,

40
00:02:32,230 --> 00:02:36,880
based on that discussion, why we would need to add in a type annotation here, even though it looks

41
00:02:36,880 --> 00:02:39,250
like there's no airs or warnings with our code.

42
00:02:40,090 --> 00:02:40,420
All right.

43
00:02:40,420 --> 00:02:41,440
So quick aside here.

44
00:02:42,220 --> 00:02:47,450
So the first thing I want to review is how the adjacent parts function works in this diagram.

45
00:02:47,500 --> 00:02:51,490
I've got some different values on the left hand side, and then we're going to imagine that we feed

46
00:02:51,490 --> 00:02:56,710
each of these different values into JSON pass and we'll take a guess at what the different types are

47
00:02:56,710 --> 00:02:57,850
that will come out of the function.

48
00:02:58,540 --> 00:03:01,340
Now, in reality, this diagram is just a little bit misleading right here.

49
00:03:01,360 --> 00:03:06,560
In reality, any time we call Jason Pass, we get back a value of the any type.

50
00:03:07,180 --> 00:03:13,180
So this column over here on the right hand side, I'm just saying this is what the like maybe the type

51
00:03:13,180 --> 00:03:13,570
would be, right?

52
00:03:13,580 --> 00:03:17,530
This is like we know that the type of false ones we pass, it would be a boolean.

53
00:03:17,540 --> 00:03:18,870
That's not what we get with TypeScript.

54
00:03:19,000 --> 00:03:20,560
This is just like the ideal world.

55
00:03:21,870 --> 00:03:27,210
So if we fed in a string of falls to Jason Pass, we get back a billion again, that's not what really

56
00:03:27,210 --> 00:03:27,630
happens.

57
00:03:27,630 --> 00:03:30,060
We get back any but hey, we're just saying we get back a billion.

58
00:03:30,780 --> 00:03:33,030
If we pass in a string of four, we would get back a number.

59
00:03:33,510 --> 00:03:38,610
If we pass in something that looks like an object with value and then five, we would get back a type

60
00:03:38,610 --> 00:03:40,410
that is an object with value.

61
00:03:40,410 --> 00:03:41,070
That is a number.

62
00:03:41,520 --> 00:03:46,860
And likewise, if we have a object with name that is pointing out a string, we would get back an object

63
00:03:46,860 --> 00:03:48,660
with a name, property that is a string.

64
00:03:49,170 --> 00:03:51,120
And you can see each of these examples very easily.

65
00:03:51,120 --> 00:03:56,850
If you open up a JavaScript console really quickly and you call, say, JSON pass with a string of like

66
00:03:56,850 --> 00:03:57,930
five, we get back a number.

67
00:03:57,930 --> 00:03:58,230
Right.

68
00:03:58,260 --> 00:03:58,950
Makes sense.

69
00:03:59,670 --> 00:04:05,940
Now, the kind of common theme here is that we can clearly pass in very different strings into this

70
00:04:05,940 --> 00:04:10,440
function and we can possibly get back very, very different value types.

71
00:04:11,220 --> 00:04:14,910
In some cases, we might get a boolean, in other cases we might get a number.

72
00:04:14,940 --> 00:04:18,420
And then in some other cases we might get these really complicated objects.

73
00:04:19,370 --> 00:04:23,810
So the goal, this diagram right here is just to help you understand that any time we call Jason Pass,

74
00:04:23,930 --> 00:04:27,990
we get back possibly some very, very different value types.

75
00:04:28,610 --> 00:04:32,540
Now, the problem here is that TypeScript cannot predict these different types.

76
00:04:32,810 --> 00:04:38,240
TypeScript doesn't know what we're going to get out of Jason Pass because it depends entirely on the

77
00:04:38,240 --> 00:04:40,490
string that we put into that function.

78
00:04:41,430 --> 00:04:47,130
So when you and I are writing code inside of our code editor and we call Jason Pass typescript, just

79
00:04:47,130 --> 00:04:53,370
plain can't predict what we get back from the function because it will be entirely different based upon

80
00:04:53,370 --> 00:04:54,300
the string that we put in.

81
00:04:54,630 --> 00:04:59,760
And TypeScript is not going to try to like read the string and figure out what it would return if we

82
00:04:59,760 --> 00:05:01,010
call Jason parts.

83
00:05:01,680 --> 00:05:07,770
So as a shortcut, typescript instead decides to simply say, you know what, I can't guess all these

84
00:05:07,770 --> 00:05:09,390
different types, that's too complicated.

85
00:05:09,690 --> 00:05:15,210
So I'm going to say, if you ever call Jason Pass, you're going to get back something called the any

86
00:05:15,210 --> 00:05:15,630
type.

87
00:05:17,220 --> 00:05:23,430
The any type essentially means that typescript has no idea what type of value is being returned from

88
00:05:23,430 --> 00:05:28,410
JSON parts, has no clue at all, and that's what we see inside of our code editor.

89
00:05:29,310 --> 00:05:34,140
So right here, when we mouseover coordinates, TypeScript is saying this is the any type, that means

90
00:05:34,140 --> 00:05:37,710
that TypeScript has no idea what type coordinates is.

91
00:05:39,530 --> 00:05:43,700
So that's what the any type indicates now, a couple more notes here on the any type.

92
00:05:44,780 --> 00:05:50,870
So any is a type, just as string or boolean are, any time we have to write out a type of a variable

93
00:05:50,870 --> 00:05:55,490
like, for example, in a type annotation, we could in theory put in any just as easily.

94
00:05:56,470 --> 00:06:00,640
Any time we see something that is marked with any type, that means that TypeScript has no idea what's

95
00:06:00,640 --> 00:06:05,040
going on and in general, that is a very bad thing to have inside of application.

96
00:06:05,650 --> 00:06:10,840
Remember the entire idea behind TypeScript and all these types and all this kind of stuff is that we're

97
00:06:10,840 --> 00:06:14,140
using TypeScript to catch errors inside of our code editor.

98
00:06:14,530 --> 00:06:20,080
And we able to do that because of these types like types like string and number and boolean and whatnot.

99
00:06:20,710 --> 00:06:25,810
If TypeScript knows the different types inside of application, it can take a look at any time we say

100
00:06:26,080 --> 00:06:30,880
a function on an object or reference a property on an object in typescript can figure out whether or

101
00:06:30,880 --> 00:06:35,410
not we are referencing a property that actually exists or if it's one that we just made a typo on.

102
00:06:36,120 --> 00:06:42,580
So any time we have a value, that is the any type typescript cannot do any er checking whatsoever around

103
00:06:42,580 --> 00:06:43,270
that value.

104
00:06:44,020 --> 00:06:47,350
So for example, right now if I go back over to my code editor.

105
00:06:48,670 --> 00:06:53,830
I can write out coordinates, dot, blah, blah, blah, blah, blah, like, so this is clearly a property

106
00:06:53,830 --> 00:06:58,990
that does not exist, but you'll see that TypeScript does nothing to help me out and tell me that I

107
00:06:58,990 --> 00:07:00,230
might have a mistake right here.

108
00:07:00,970 --> 00:07:05,500
However, if I go back up to any of these other examples where I'm working with variables that have

109
00:07:05,500 --> 00:07:08,680
known type like let's say how about speed right here?

110
00:07:09,490 --> 00:07:13,990
If I reference speed, blah, blah, blah, TypeScript is going to jump in very quickly and say, hey,

111
00:07:13,990 --> 00:07:15,730
that's not a property that exists.

112
00:07:16,210 --> 00:07:21,340
It's only going to allow me to call properties that actually exist like, let's say to upper case like.

113
00:07:21,340 --> 00:07:22,450
So that's fine.

114
00:07:22,600 --> 00:07:25,480
And it's only fine because TypeScript knows that speed is a string.

115
00:07:26,380 --> 00:07:32,020
So, again, any time we have a variable of type, any, it is generally a bad thing because TypeScript

116
00:07:32,020 --> 00:07:33,570
can't do its job.

117
00:07:34,690 --> 00:07:39,040
All right, so I think that we've gone into enough detail on this any type, so let's have another quick

118
00:07:39,040 --> 00:07:39,570
pause right here.

119
00:07:39,580 --> 00:07:44,050
We're going to come back to the next video and understand how we can use a type annotation to fix this

120
00:07:44,050 --> 00:07:44,950
thing up right here.

121
00:07:45,070 --> 00:07:47,010
So quick pause and I'll see you in just a minute.

