Alright, so in this video we're going to talk about types. So 1
with typescript you have several types. But we're going 2
to first work with primitives. So primitives are the most 3
basic types, okay? We have number, string and boolean in 4
lowercase, that's very important. And then 5
where we're going to discover other types, okay? So these are 6
I would say the the everyday types that we're going to use, 7
okay? So all of these ones, so the primitives and this part of 8
the advanced types. So arrays, objects, functions, 9
enameration, null and undefined and void. Obviously we're going 10
to cover all these types. And then we have more occasional 11
types so unknown never any so any is very special because 12
that's one of the types that you should almost never use is 13
very famous for not being liked by the typescript community 14
because when you're writing any it basically means "I don't want 15
to use typescript" this can have any types so it it's 16
basically like not using typescript but we're going to talk 17
about that later and obviously there are other types a lot 18
more that are more advanced and you can even create your own 19
types but we're going to talk about that later in the course. 20
For now we're going to work with the primitives. So you can 21
go back in your project. Uh and by the way if you want to be 22
able to start your project very simply what you can do is you 23
can go in your package dot json and you can create a 24
start comment that is basically going to do what we do when we 25
start the project. So a TSC then you're going to target the 26
index. TS file and just write --watch this way now you 27
can just type NPM start and it's going to start a project 28
and so let's say this is closed now if I click on go live at 29
the bottom since now my projects in source I have to 30
click here to open it for now and now I'm in the index. HTML 31
file okay so now we can go into our index. TS and I'm just 32
going to transform this awesome variable into a let so I can 33
reassign it so to type a variable you can do it like 34
this I can just do colon string like this and now my 35
awesome variable is a string it's a string so now I cannot 36
do something like this okay this is not going to work and 37
you have to learn how to read type script errors so it's 38
going to say that the type a number so the number one is not 39
assignable to the type string okay so you cannot put a number 40
into a string so now you have a validation , typescript is 41
going to help you know when you're doing something wrong so 42
you cannot do that for example you could declare a number so 43
I'm going to call it for example value and it's going to 44
be a number like this and it's going to be one okay and again 45
you cannot do something like this it's not going to work so 46
you can declare a number like this and you can even declare 47
for example is cool variable boolean is going to be true 48
like this so now on each of my variable I have a type that is 49
affected and I can do awesome dot and I will have only the 50
functions that are related to string that you can run on a 51
string so for example to lowercase and now the cool 52
thing is that you even have syntax checking so for example if I 53
misspell this I will have some help and it's going to say that 54
to local "lowerase" does not exist on type string but maybe 55
I meant local lowercase so it's going to try to find the best 56
match for what you wrote so also this is going to work okay 57
and you can even use the quick fix and I'm going to say "change 58
spelling" and it's going to fix it for me if I want okay so 59
that's another cool feature of so now example here in my 60
paragraph if I try to inject something else than a string I 61
should have an error because if you pass your mouse over inner 62
text you're going to notice that inner text is a string so 63
if I try to for example put value in it it's not going to 64
work and again you will have an error that says number is not 65
assignable to type string okay so it's important that you 66
learn how to read type script errors okay sometimes they are 67
not really meaningful but most of the time they are so it's 68
important to understand what's going on okay so that's how you 69
declare a type for a variable but now you have to know 70
something what we have done is not really useful so I'm going 71
to fix that you don't really have to explicitly tell the 72
type here on a primitive I could actually remove it like 73
this and you're going to say why what's the point so I don't 74
need types on primitive ? No you don't. Because Typescript is 75
smart enough to detect what is the type of the variable if a 76
primitive was assigned so here as you can see it understood 77
that it's going to be a string here it understood that it's 78
going to be a number and here a boolean so I still have my type 79
safety okay I still cannot do something like this: awesome 80
equal true for example this is not going to work so with 81
primitives there is something called type inference and it's 82
basically the behavior of type script trying to find logically 83
what is going to be the type of your variable okay so you don't 84
really need to specify especially here the type but 85
you'll see that we will have to do that sometimes and mostly when we 86
will create our own types alright But for now, we're 87
good. You don't need to do that. So this is actually 88
pretty much it about primitives, okay? You don't 89
really have to do anything, they're just typed by default. 90
Thanks to the type inference of typescript. Alright, so see 91
you in the next one to continue.