Types You Don't Control 12 exercises
explainer

Integrating TypeScript with Node

Now that we've talked about Window, Document, and other DOM-related objects, let's talk about typing Node.js.

To get TypeScript working with Node, you need to install @types/node:


pnpm i @types/node

Without installing the @types/node package, you're cut off from global objects

Loading explainer

Transcript

00:00 We've talked a lot about Window, about Documents, and things like that, but we've not talked so much about Node. Node is like a back-end framework, a back-end environment, where you can run TypeScript or run JavaScript on the server, on desktop applications, on all sorts of different places. And to get it working with TypeScript,

00:19 you need to do one thing and one thing only, which is to install TypesNode here. Without TypesNode, we lose access to globals, like Process. We also lose access to modules that just come bundled with Node, like FS, FS Promises, things like this.

00:35 And so we would lose type-safe access to read file or things like this. And so without this, these would all be red squiggly lines. Now, this is really, really useful, and it's kind of interesting to me that you have Node, on one hand, sort of maintained by a community of people and a committee of people, and then, on the other hand, you have the types for Node

00:55 living in a separate repo, which you can download as needed. And of course, the community of people from Node are very involved in those type definitions and working out how those should live. And so TypesNode is a really invaluable thing to know about if you're working in Node.