Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: TypeScript Motorcycles
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's compare this motorcycle application, if you can call it that, that we wrote in Python without types to some of the other languages that we saw on the spectrum.
0:12
So the first one on our list over here is TypeScript. So let's just start from the top and go through TypeScript.
0:18
So in Python, we have a class Motorcycle. In TypeScript, we have a class Motorcycle. Remember, this is not JavaScript. This is TypeScript.
0:27
The JavaScript is quite intense. But luckily, we don't have to write that. So we have a model, a style, engine size, and off road.
0:35
Model style, engine size, off road. By charm kind of added them in reverse the way we were doing that there.
0:42
Over in TypeScript, you can see the model is a string, the engine size is a number and
0:47
the style we've gone and added something more explicit enumeration. Yeah, we could have done that in Python as well.
0:54
But when it's really untyped, it's not very common to do that. We'll move to that when we get our typed version potentially.
1:01
We got different types of motorcycles, one of them being adventure, that we can use and it's always going to be consistently the same.
1:07
So that's pretty cool. And then we have a constructor just like the initializer there.
1:12
We have a model, string, style, motorcycle type, engine number, and off-road boolean. But otherwise, this versus self, it's the same thing.
1:23
a can jump return this off road, create an adventure, just like before, pass in the model and the engine size.
1:34
Otherwise we're using adventure and off road right but you can see this type information
1:38
here if I was to pass for offer type yes, come back here you immediately it says string is not a Boolean. You've done it wrong. There you go.
1:50
As opposed to... Yes. Okay. I mean, I guess they can still jump. But if I said no, I guess they can still jump. That's weird.
2:07
Right? There's no help over here. Right? It's untyped. Also, let's scooch this over a little.
2:16
down here and create some motorcycles. So it looks really, really similar. I would think
2:20
for you at the very top, I put the steps that you need to actually compile this or get it
2:25
to go. So you have to have node installed to run this one. And it says CD code, chapter
2:33
two. Here we go. And in this we have our package dot JSON. So then we can just try to run this it'll say, ""Hold on, hold on, hold on.
2:46
In order to do this, you have to have the TypeScript compiler, the TSC, so I'll let Node do that. npm install should probably do it, actually.
3:00
And we'll just double check TypeScript. There we go. npm install TypeScript.
3:04
That's all good, and now we should have our Node packages up here, as we do. Now we can run mpx tsc motorcycle.ts. No output. I guess that that's good.
3:17
But if you go up here, notice a little chevron appears and we get the red pill of TypeScript, if you will. So you saw what we wrote.
3:28
But look at this bad boy. Look how you define enumerations in pure JavaScript or a class, which is a function and the whole
3:35
evaluation is the constructor. right, you can see the prototypical inheritance stuff going on, there's the prototype, for
3:44
example. Alright, so that's kind of funky there. But you know, it's okay, we don't have
3:50
to write it. That's the compilation step. And now we have this, what does it say to
3:55
run it, we just say node and the JavaScript file that was created there, some bikes. And
4:02
And look, it's exactly the same other than capital T versus lowercase t.
4:07
So there's the difference between untyped Python, right, this version, like this. And we got our TypeScript like this. The TypeScript is pretty clean.
4:22
But as we saw, the JavaScript is super gnarly because they're wrapping runtime behavior
4:28
that models what we created in TypeScript into a language that's super, super not built for that. But you don't have to look at it, I guess it's okay.