Loading
Current section: 3. Primitive Types 8 exercises
solution

Numbers And Strings

Transcript

00:00 All right. Let's go ahead and get this one done. So we have our variable named price. We're going to set that to $29.99. That's going to have a type of number.

00:09 Now, we can be explicit and say number is our type, to be very explicit. We'll talk about inference here in the next exercise. So this is absolutely gotta be assigned to a number. It can't be assigned to anything else. Hi there.

00:25 We can't assign it to anything else. We can do this as anything that that the ghost text is saying. You don't really wanna do that normally. So we're gonna not do that and instead we're gonna set it to 2999. Then we're gonna create a product name and this we are going to also do that colon and then the the name of the type.

00:45 The name of the type is string. You don't need that extra space there after the colon. You definitely need the colon though. That changes things entirely. It would change the variable name.

00:54 But, yeah. So we're gonna do colon string equals, the typescript guide. And this means we can't set it to a number because now a typescript is like, hold on. Hold on. You said the product name was a string, but now you're over here assigning it to a number.

01:08 Come on, man. Like, what's going on here? So we're gonna assign it to a string. And then we can create a, quality is a number and, set that to a 100. And then we're gonna create a description, which is a string that's a template literal.

01:22 So it's still same same sort of string even though this one's single quote and this one's a back tick with interpolated values and everything. This still ultimately is a string. So we can assign it as a string and give it that very explicit type. And then, we take care of that. Let's uncomment these and uncomment this.

01:43 And we take a look at our results here. We've got our price as $29.99. TypeScript guide quality is 100. And here's the, results of our description. Product typescript guide.

01:55 Price, $29.99. In stock, 100. And then here are all of our exports which will be used by the test. So there you go. You can explicitly say what type a variable can be.

02:06 And the cool thing about this too is if I were to say, hey. Let this price be a number and it is assigned to this. Now later on, I can't say price equals, $30.99. TypeScript is gonna warn me about that. This is the value of, TypeScript and and being able to specify the type of something.

02:26 We're gonna actually improve this, fair bit because there's a lot of typing, pun intended, to make all of this work. And, TypeScript's a little bit smarter than that. So we'll take a look at that here in the next exercise. But for now, well done on this one.