TypeScript - Installation & Toolchain
Install TypeScript
npm init -y
npm install --save-dev typescript
npx tsc --init
This creates tsconfig.json
. You'll tune it later in the tsconfig Deep Dive.
Compiling
- tsc: Official compiler, typechecks and can emit JS.
- tsx: Fast runner for TS/ESM without config; great for scripts.
- esbuild/tsup/swc: Extremely fast bundlers/transpilers; often paired with tsc for typechecking.
- Vite/Rollup/Webpack: App bundlers with TS support via plugins/loaders.
Scripts
{
"scripts": {
"build": "tsc -p tsconfig.json",
"watch": "tsc -w",
"dev": "tsx src/index.ts",
"typecheck": "tsc -p tsconfig.json --noEmit"
}
}
Recommended tsconfig starting point
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}
Node ESM/CJS
Decide ESM vs CJS per project. For ESM in Node, set "type": "module"
in package.json and use NodeNext resolutions when needed. We'll deep dive later.