1
00:00:00,870 --> 00:00:04,920
Now that we've got a better understanding of what a type is, let's dive a little bit deeper into the

2
00:00:04,920 --> 00:00:07,890
subject and then discuss why we care about types at all.

3
00:00:08,520 --> 00:00:08,880
All right.

4
00:00:08,880 --> 00:00:11,960
So first off, a couple examples of some very basic types.

5
00:00:12,720 --> 00:00:17,970
So any time that we are working with a value that has a type of string, we would expect to see a value

6
00:00:17,970 --> 00:00:19,180
that look like these right here.

7
00:00:19,650 --> 00:00:23,190
So if I have a value that looks like high there, that is a string.

8
00:00:23,730 --> 00:00:28,380
If I see a value that looks like a string with no characters, that is also string and something that

9
00:00:28,380 --> 00:00:29,390
says today's Monday.

10
00:00:29,400 --> 00:00:30,570
Yep, that's a string as well.

11
00:00:31,900 --> 00:00:36,880
Likewise, if I've got some really small decimal number, that's a number, a negative number has a

12
00:00:36,880 --> 00:00:39,510
number and a really large number is a number as well.

13
00:00:40,770 --> 00:00:45,660
And the reason I break out no right here is that in some other languages, there are actually subtypes

14
00:00:45,660 --> 00:00:46,230
of numbers.

15
00:00:46,260 --> 00:00:51,360
So for example, in other languages, you might have a type to represent numbers that have a decimal

16
00:00:51,780 --> 00:00:56,490
or you might have another type to represent a number that is only an integer and therefore doesn't have

17
00:00:56,490 --> 00:00:57,000
a decimal.

18
00:00:57,980 --> 00:01:03,350
In JavaScript or me in typescript, we have nothing like that, a number is a number, regardless of

19
00:01:03,350 --> 00:01:08,010
whether it is positive or negative, has a decimal, doesn't have a decimal there.

20
00:01:08,030 --> 00:01:08,660
All numbers.

21
00:01:10,050 --> 00:01:15,090
If we have a value of true or false, we would say that its type is Boolean and then remember, we are

22
00:01:15,090 --> 00:01:20,730
not only restricted to these basic types right here, every value in typescript has a type.

23
00:01:21,330 --> 00:01:27,120
So, for example, if we made a new date object, the resulting object from that would have a type of

24
00:01:27,240 --> 00:01:27,750
date.

25
00:01:28,530 --> 00:01:35,280
And then if we had an object that had an ID, a completed flag, a title flag, we would say that that

26
00:01:35,280 --> 00:01:37,500
is an object that has type to do.

27
00:01:37,980 --> 00:01:42,680
And this one only applies if we were working in a code base that had to find this interface right here.

28
00:01:42,690 --> 00:01:44,730
Remember this interface to find a new type.

29
00:01:46,080 --> 00:01:48,990
OK, so now we've seen some examples of different types.

30
00:01:49,260 --> 00:01:52,080
Let's talk about how all these types kind of relate to each other.

31
00:01:53,350 --> 00:01:58,450
So in the world of TypeScript, we have two different categories of types, we have primitive types

32
00:01:58,450 --> 00:01:59,710
and object types.

33
00:02:00,490 --> 00:02:03,880
Primitive types are all the very basic types that you might get.

34
00:02:03,910 --> 00:02:10,060
So like no strings, boolean null, undefined and then Simbel and void are two others we'll talk about

35
00:02:10,060 --> 00:02:10,570
later on.

36
00:02:12,330 --> 00:02:17,640
The other category we have access to are something called object types, and these are essentially any

37
00:02:17,640 --> 00:02:22,080
types that you and I create or any other types that are built into the language itself.

38
00:02:22,230 --> 00:02:26,520
So, for example, all the different functions that you and I create are going to have their own type.

39
00:02:26,910 --> 00:02:31,800
And those are a object type, any class you and I create or he's going to have its own type.

40
00:02:32,130 --> 00:02:34,350
Same thing with arrays and objects as well.

41
00:02:36,190 --> 00:02:40,660
Now, why is there the divide here, like why are there two categories, one called primitive types

42
00:02:40,660 --> 00:02:41,980
and one called object types?

43
00:02:42,550 --> 00:02:45,550
Well, the true answer to this, we won't quite see until later on.

44
00:02:45,550 --> 00:02:50,740
But just as a quick preview, we say that there is a separate category called object types right here,

45
00:02:50,740 --> 00:02:56,770
essentially because you and I can write some code that will essentially trick TypeScript into believing

46
00:02:56,770 --> 00:02:59,240
that one value is a different type.

47
00:02:59,620 --> 00:03:04,450
I know that sounds really confusing right now, but essentially we can kind of sometimes trick the typescript

48
00:03:04,450 --> 00:03:06,280
compiler in a very good way.

49
00:03:07,130 --> 00:03:09,040
We can do that with object types only.

50
00:03:09,190 --> 00:03:11,110
We can not do that with primitive types.

51
00:03:11,120 --> 00:03:13,330
So that's why we have these two separate categories.

52
00:03:13,930 --> 00:03:15,910
Again, I know that's confusing to hear right now.

53
00:03:16,030 --> 00:03:20,620
I just want to understand why you will sometimes see these broken out into the two separate categories.

54
00:03:21,770 --> 00:03:26,930
OK, so now that we understand a little bit more about type's until the last big question here, why

55
00:03:26,930 --> 00:03:29,050
do we care about Type's at all?

56
00:03:29,780 --> 00:03:32,600
If I had to answer that, I would say that there are two big reasons.

57
00:03:32,750 --> 00:03:37,580
The first one is that types are used by the types of compiler to analyze our code for errors.

58
00:03:38,210 --> 00:03:40,370
So think about the definition that we just gave or types.

59
00:03:40,370 --> 00:03:45,320
We just said that types are an easy way to refer to the different properties and functions that a value

60
00:03:45,320 --> 00:03:45,800
has.

61
00:03:46,370 --> 00:03:50,660
Just keep that in mind for a second while you keep in mind I'm going to flip back over my code editor

62
00:03:50,660 --> 00:03:52,930
and take a look at the code that we just wrote a little bit ago.

63
00:03:53,780 --> 00:03:56,360
So inside of here, we defined a new type.

64
00:03:56,750 --> 00:04:02,810
We said that we had a new type inside of application called to do and a to do is only ever going to

65
00:04:02,810 --> 00:04:05,840
have the properties ID title and completed.

66
00:04:06,980 --> 00:04:14,060
Then down here on line 12, we said that the variable to do was going to referred to an object of type

67
00:04:14,060 --> 00:04:19,550
to do so, that means that the object that this variable refers to is only ever going to have these

68
00:04:19,550 --> 00:04:20,180
properties.

69
00:04:20,839 --> 00:04:23,430
The typescript compiler can use that information.

70
00:04:23,930 --> 00:04:29,660
So if we start to refer to some property on that, to do variable that doesn't exist, TypeScript is

71
00:04:29,660 --> 00:04:30,770
going to throw in aretz.

72
00:04:31,220 --> 00:04:36,890
TypeScript knows that this variable refers to an object that has these properties and nothing else.

73
00:04:37,400 --> 00:04:41,960
So if we try to reference some other property, there must be something wrong with our code in.

74
00:04:41,960 --> 00:04:45,470
TypeScript can discover that and tell us that in the form of an error.

75
00:04:46,470 --> 00:04:48,630
So that's why we generally care about types.

76
00:04:49,840 --> 00:04:51,640
The other big reason that we care about types.

77
00:04:52,770 --> 00:04:58,980
Back over here, one more is that allows it allows other engineers to understand what values are flowing

78
00:04:58,980 --> 00:05:00,110
around in our code base.

79
00:05:00,630 --> 00:05:05,280
If you've ever worked on a large JavaScript project before, you might have ran into the scenario where

80
00:05:05,280 --> 00:05:10,920
you open up some file and you see some function in there with some really unclear argument name like

81
00:05:10,920 --> 00:05:14,780
maybe a function with an argument name of like the letter A and nothing else.

82
00:05:15,390 --> 00:05:20,340
In that case, on a large project, it might be really hard for you to understand what that function

83
00:05:20,340 --> 00:05:20,910
is doing.

84
00:05:21,920 --> 00:05:28,700
If we instead tried to annotate the type of arguments being passed into a function by labeling the types,

85
00:05:29,000 --> 00:05:34,280
then that at least gives other engineers some idea of what type of data is flowing around inside of

86
00:05:34,310 --> 00:05:34,970
application.

87
00:05:35,420 --> 00:05:39,260
And ultimately, it will make it slightly easier for other engineers to understand what our code is

88
00:05:39,260 --> 00:05:39,590
doing.

89
00:05:40,730 --> 00:05:44,540
So that's why we care about types, let's now take a quick pause right here.

90
00:05:44,540 --> 00:05:48,680
When we come back, the next video, we're going to start to take a look at some more code snippets

91
00:05:48,680 --> 00:05:50,270
to understand what's really going on.

92
00:05:50,480 --> 00:05:52,390
So quick pause and I'll see you in just a minute.

