TypeScript: From First Steps to Professional

DefinitelyTyped

Anjana Vakil
Software Engineer & Educator
TypeScript: From First Steps to Professional

Lesson Description

The "DefinitelyTyped" Lesson is part of the full, TypeScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana introduces Definitely Typed as a valuable resource for acquiring missing type declarations and demonstrates how to install type declarations for common Node.js dependencies using npm.

Preview
Close

Transcript from the "DefinitelyTyped" Lesson

[00:00:00]
>> Anjana Vakil: We now have gotten our first taste of like, oh, something's wrong here and it's not my code, it's the fact that TypeScript doesn't have enough types So essentially, TypeScript's got a fever, and the only prescription is more npm i -D type stuff So we need to grab more types for all of those dependency libraries that we're using, which are not out of the box giving TypeScript declaration files that it can use to understand what kinds of things these libraries come with

[00:00:45]
And so that's where there's this cool initiative called Definitely Typed, which is essentially kind of like the missing types for the JavaScript ecosystem And so if we go to GitHub, Definitely Typed, Definitely Typed, you can read more about this There's some danger, but essentially, this is sort of like what are declaration files How do I get declaration files for packages that are missing them

[00:01:26]
And what this project does is essentially try to define types that we need to work with these many common tools And so that's what that @types thing was that we just installed We just did this We used npm to install some type declarations that are packaged up for us, put a little bow on it for us, by this Definitely Typed project, and we can do this with a whole bunch of libraries because this is an open source thing and it is partly the fact that we have such a huge community of other developers who are working in TypeScript who are like, I really need some type declarations for this library, and nobody made them, so I am going to write them and I'm going to publish them for the world to enjoy

[00:02:29]
So you can read a lot more about this, but suffice it to say if I go back to my slides, yeah, we can install types for Node with @types/node, and we can install types for pretty much all of the very common Node server-side dependencies that we're using in this app with a similar format So basically, we can put an @types in front of the package name that's in our dependencies and install that to grab some type declarations for each of these non-TypeScript dependencies that we're using

[00:03:15]
So for example, Express is the server framework tool that we're using to serve this extremely exciting API and so we can install the types for that with npm i -D @types/express Let's keep an eye on our package.json, and let's try that out So we're going to clear npm i -D @types/express And boom Now it's there And now if we go over to server, one down, a whole bunch to go, so let's look at our package.json, and look at all of these packages and try to get types for all of them and see if it makes TypeScript happy

[00:04:09]
OK, so we're just going to, you know, ideally we'd want to like check and see if these are things, but we're developers, we don't have time for that So we're going to just put an @types in front of pretty much every package I see here Let's try CORS, for example Oh, that worked cool Let's try cookie-parser Cool, let's try Helmet Cool Look at all these types

[00:04:40]
And we got something called Morgan, and you can read up on what all these things are and what they do The point is we have a bunch of JavaScript packages that don't come with types, and yet the community delivers They put deliver first after teaching and playing, which is the slow play, play, teach, deliver OK So if I do npm i -D @types/morgan, that also works, and let's look here and we're getting there

[00:05:12]
We are getting there And just a side note that we mentioned it earlier, but Scott Moss works with Express and his API design with Node.js course uses Morgan and Helmet and sets up routes and handles CORS and all that Awesome, yes, so this is not about what any of these modules do It's just about what kinds of stuff they give us and how we know what types it has

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now