TypeScript Survival Guide

Favorite typescript videos

154 — TypeScript Setup for Node.js (VS Code, Inference, @types, PM2)

Full companion narrative for Zaiste Programming's Node TypeScript setup — the type system pitch, transpile versus compile, inference discipline, @types/node, and PM2 for production

Video: 154 · TypeScript Setup for Node.js with VS Code & PM2 | ~15:55 lesson | watch · Zaiste Programming

Source: this is a written companion to the video. Part of the Favorite TypeScript Videos curated path, module 00 (foundations) — the typed production setup: 153 gives the platform its history, and this video makes it type-safe and deployable. Forward, 155 layers Express onto the same foundation.

“Let’s see how we can set up a Node.js project with TypeScript. In addition to that we will be using Visual Studio Code exclusively.” The video’s pipeline has three stages: write typed code with editor support, transpile to JavaScript Node actually runs, and manage the process with PM2. Each stage gets its own tooling, and the lesson wires all three.

empty VS Code workspace at the start of the typed Node setup

the pitch: the type system is the feature

“The biggest feature is the type system which means that you can add types to your programs” — the annotated message plus the tsc-then-node terminal pair in the frame show the deal: types exist for the compiler and the editor, then erase before execution. The same erasure 147 demonstrates at emit level.

// types for the compiler, JavaScript for the runtime
let message: string = "Hello, World";
console.log(message);

annotated message with the tsc plus node terminal pair

transpile, do not compile — and let inference work

“Transpilation is a special case of compilation.” TypeScript-to-JavaScript is source-to-source, not lowering to machine code — a distinction that explains why the output stays readable. Then the discipline: “You should let TypeScript compiler to guess the types for you.” The unannotated message beside the annotated compute shows both modes: infer locals, annotate boundaries.

// infer locals, annotate function boundaries
let message = "Hello, World";
console.log(message);

function compute(a: number, b: number): number {
	return a * b;
}
console.log(compute(2, "a")); // type error, caught before run

inferred locals beside annotated compute with its caught error

the gap: Node ships no types, DefinitelyTyped fills it

“Because Node.js is not written in TypeScript, there is no type information.” The http server imports cleanly only after the fix — “there is this project called Definitely Typed, which provides type information to” untyped JavaScript. One devDependency closes the gap, the same install 149 and 150 perform for Express and styled-components.

// typed server — possible only with the @types package installed
import http from "http";

http.createServer((req, res) => {
	res.end("Hello, World!");
}).listen(3000, () => {
	console.log("Server started.");
});
npm install --save-dev @types/node

DefinitelyTyped site as the source of Node type information

the server, typed end to end

The wired server frame shows the payoff composed: http import, createServer, listen on 3000 — every identifier resolved against the installed declarations. Compare with 152’s untyped six-liner: same runtime behavior, plus compiler-verified wiring.

typed http server with createServer plus listen wired

production: PM2 runs the built output

So this is for production — and PM2, which also provides a daemon process manager, starts the compiled dist/main.js with clustered instances (-i max) — process management, restarts, and scaling that plain node never offers. The lesson’s arc completes here: typed source, compiled output, managed process.

pm2 start dist/main.js -i max

production server with the PM2-managed terminal below

The practice hinge of the Node run: backward it types 152’s server and leans on 149’s @types mechanism; its inference discipline echoes 135. Forward, 155 replays the same pipeline (install, init, scripts, production) with Express added.

Prev: 153 — Node.js, The Documentary · Next: 155 — TypeScript with Express and Node

Transcript notes: Quotes (“Let’s see how we can set up a Node.js project with TypeScript. In addition to that we will be using Visual Studio Code exclusively.”, “The biggest feature is the type system which means that you can add types to your programs”, “Transpilation is a special case of compilation.”, “You should let TypeScript compiler to guess the types for you”, “Because Node.js is not written in TypeScript, there is no type information.”, “there is this project called Definitely Typed, which provides type information to”, “So this is for production”) verified against the transcript segments; the “PM2 death” ASR fragment is paraphrased above as a daemon process manager, not quoted verbatim; the annotated snippets, install commands, and server code verified against the frame captions. Screenshot refs cover 6 sampled frames in order (source frames 1, 6, 8, 9, 11, 16): frame-0001.png (empty workspace) through frame-0006.png (PM2 production) across the ~15:55 runtime.