Installing TypeScript
Install TypeScript per-project, not globally. Pin the version in package.json so CI and every developer's machine use the same compiler.
Quick install
SHELL
# In a project root npm init -y npm install -D typescript @types/node npx tsc --init # generates tsconfig.json
Why -D (dev dependency)
You only need TypeScript at build / test time. At runtime, the deployed JavaScript runs without it. Installing as devDependency keeps production installs lean.
Scripts to add
package.json
{
"scripts": {
"build": "tsc",
"build:watch":"tsc -w",
"type-check": "tsc --noEmit",
"start": "node dist/index.js",
"dev": "tsx watch src/index.ts"
}
}
Other package managers
| npm | pnpm | yarn | bun |
|---|---|---|---|
npm install -D typescript | pnpm add -D typescript | yarn add -D typescript | bun add -d typescript |
Useful supporting packages
| Package | Why |
|---|---|
@types/node | Types for Node's built-in modules. |
tsx | Run .ts files directly — modern ts-node replacement. |
typescript-eslint | ESLint + TS rules. |
@tsconfig/node20 (or your version) | Curated tsconfig preset. |
vitest | Fast TS-friendly test runner. |
Editor — use the workspace TS version
VS Code: Cmd/Ctrl+Shift+P → "TypeScript: Select TypeScript Version" → "Use Workspace Version". Otherwise your editor's bundled TS might be older or newer than CI's.
Tip: Pin the exact version with
"typescript": "5.6.2" (no caret). One less moving part across machines and CI — and TS minor releases occasionally tighten checks.Example
Example
// Local install (recommended):
// npm install -D typescript @types/node
// npx tsc --init
//
// Or pnpm / yarn — same packages.
// 'tsc -w' watches for changes; 'tsc --noEmit' just type-checks.
console.log('Pin TS in package.json so CI uses the same version');
Try it Yourself »
Exercise
Install TypeScript as a dev dependency.
npm install
typescript
Two characters; flag for devDependency.
Discussion
Loading…