TypeScript / Node.js — Basics
Runtime
Section titled “Runtime”- V8 + libuv + Node bindings. JS single-threaded, I/O via libuv thread pool (default 4,
UV_THREADPOOL_SIZE). - libuv uses epoll / kqueue / IOCP for network. Thread pool for file + DNS.
Event loop phases
Section titled “Event loop phases”- timers —
setTimeout/setInterval - pending callbacks — deferred I/O
- idle/prepare — internal
- poll — fetch I/O events
- check —
setImmediate - close —
socket.on('close')
Microtasks (process.nextTick, Promise .then) drain between phases. nextTick before Promises. Recursive nextTick starves I/O.
Streams
Section titled “Streams”4 types: Readable, Writable, Duplex, Transform. Always use pipeline() (handles errors). Modes: flowing vs paused.
TypeScript essentials
Section titled “TypeScript essentials”- Structural typing (shape match).
anydisables checks.unknownrequires narrowing.never= impossible.type= unions/intersections/mapped/conditional.interface= extensible via merging.- Narrow via
typeof,instanceof,in, type guards (x is Foo), discriminated unions. - Utility:
Partial,Required,Readonly,Pick,Omit,Record,Exclude,Extract,ReturnType,Parameters,Awaited. - Generics with
extendsconstraints. ConditionalT extends U ? X : Y.inferextracts. - Mapped:
{ [K in keyof T]: ... }. tsconfig:strict,noUncheckedIndexedAccess,target,module,moduleResolution: NodeNext.
asyncreturns Promise.awaitsuspends.Promise.all(fail-fast),allSettled,race,any.- Don’t
awaitin loop for independent items —Promise.all(arr.map(...)). - Unhandled rejection crashes Node 16+.
Modules
Section titled “Modules”- CJS (
require) vs ESM (import). ESM async, no__dirnamedefault. package.json"type": "module"switches default.
Tooling
Section titled “Tooling”- npm / pnpm / yarn.
npm ciuses lockfile exact. - Semver:
^1.2.3minor+patch,~1.2.3patch only.