Essential Types And Notations 20 exercises
solution

TypeScript Basic Types

Each of the examples represents the TypeScript's basic types, and would be annotated as follows:


let example1: string = "Hello World!";
let example2: number = 42;
let example3: boolean = true;
let example4: symbol = Symbol();
let example5: bigint = 123n;

We've already seen `strin

Loading solution

Transcript

00:00 Okie doke, so we know that string is correct. What should we do this one? Well, we know this one from a previous exercise, number. Okay, that's working beautifully now. So this error has gone away because example two is supposed to be a number. Example three, we can change this to Boolean instead. That's another one of the basic types.

00:17 And so this can either represent true or false here. Example four, what's this gonna be? Well, we can look at the error, type symbol is not assignable to type string. So let's take a look at that. If we just put symbol there, that's a globally available type and it works beautifully. And then finally, we have big int type.

00:35 So big ints are slightly different from numbers. They have a slightly different setup here, and you can use a type to distinguish number from big int. So we can't, for instance, pass 42 into this slot because it's a number instead of a big int. And these are the five basic types of TypeScript really. And there's a second solution here too,

00:53 which is you don't actually need to type these. We can see that example one here actually gets inferred as string just from usage. Same with this, same with this, and same with this, same with this. So all of those examples get typed as their proper types just from having the variable assigned to them.

01:14 So why would we do this? Well, this was just a fun little exercise just to get you to understand those five different types, because they're very useful when you're annotating functions. For instance, if you want a function to be able to receive just a big int instead of a number, then you can use that type instead. So knowing all of these five types is really useful,

01:31 but we don't always need to assign types to variable declarations. Very cool.