Explain it simply
Setup in plain words
To start using TypeScript you need Node.js and npm (npm comes with Node.js) on your computer. The TypeScript compiler (`tsc`) is itself an npm package. Its job is to read `.ts` files, check their types and write `.js` files, which Node.js then runs as usual.
So the workflow has three steps: write `.ts`, compile with `tsc`, run with `node`. If you want to try things without installing anything, the official TypeScript Playground in your browser shows the generated JavaScript and any errors as you type.
Problem → Why → Solution
The problem: "it compiles on my machine"
Most tutorials online say `npm install -g typescript`, then `tsc app.ts`. That works while you experiment alone, but in a team two problems appear.
The first is versions: your machine has one TypeScript, a teammate's has another, the CI server a third, and the errors they report can differ. The second is stranger: when you give `tsc` a file name on the command line, it does not read the project's tsconfig.json at all. Your `strict` and `outDir` settings are silently skipped and `app.js` lands right next to `app.ts`.
npm install -g typescript # whatever version was newest that day
tsc src/app.ts # a file name => tsconfig.json is ignored
ls src # app.js app.ts (not in dist/)Why this happens
A global install leaves no trace in package.json, so nobody knows which compiler version the project expects. And `tsc` with a file name means 'compile just this file with default options'. The fix is to install the compiler as a devDependency and run `npx tsc` with no file name.
Core concept: a per-project setup
Each project should carry its own compiler version and its own settings. The version is recorded in package.json as a devDependency, and the settings live in tsconfig.json. Anyone who clones the repository then gets the same compiler with a single `npm install`. Work through these steps in a terminal, one at a time.
Check Node.js
Run `node -v` and `npm -v`. If both print a version number you are ready; if not, install the LTS version from nodejs.org.
Create a project and install the compiler
In a new folder, run `npm init -y` to create package.json, then `npm install --save-dev typescript`. The compiler goes into `node_modules` and its version is recorded in package.json.
Create tsconfig.json
`npx tsc --init` writes a commented tsconfig.json. Enable `"rootDir": "./src"` and `"outDir": "./dist"` by deleting the `//` in front of them. `npx` finds and runs the `tsc` installed inside the project.
Compile and run
Write `src/app.ts`, then run `npx tsc` without a file name. It reads tsconfig.json and writes `dist/app.js`. Finally run `node dist/app.js`.
node -v && npm -v
mkdir hello-ts && cd hello-ts
npm init -y
npm install --save-dev typescript
npx tsc --init # then enable rootDir and outDir
npx tsc # src/app.ts -> dist/app.js
node dist/app.jsKeep in mind that the `.ts` files in `src/` are the source you edit, and the `.js` files in `dist/` are output the compiler writes. Never edit `dist/` by hand; the next compile overwrites it.
Open the generated tsconfig.json and read it once. It is mostly comments explaining each option, and `"strict": true` is already switched on, which is what you want. The next lesson goes through the important options one by one. For now, the only lines you change are `rootDir` and `outDir`, so the compiler knows where your source lives and where the JavaScript should go.
- src/app.ts (you write it)
- npx tsc + tsconfig.json
- dist/app.js (generated)
- node dist/app.js
Node.js runs the JavaScript in `dist/`, not the TypeScript in `src/`.
General pattern: project layout
my-project/
package.json <- "typescript" in devDependencies
tsconfig.json <- rootDir: ./src, outDir: ./dist
src/
app.ts <- you edit these
dist/
app.js <- tsc writes theseKeep source in `src/` and compiled output in `dist/`. `dist/` usually goes into `.gitignore`, since it can be regenerated at any time.
The first file you compile
To check that the setup works, write this `src/app.ts`, compile it with `npx tsc` and run it with `node dist/app.js`. The program prints the very setup steps you just followed.
After compiling, open `dist/app.js`. You will find no trace of `type SetupStep` or `: SetupStep[]`, only the array and the loop. Then delete one `purpose` and run `npx tsc` again to see the error.
// src/app.ts - the first file you compile with tsc
type SetupStep = { command: string; purpose: string };
const steps: SetupStep[] = [
{ command: "npm install --save-dev typescript", purpose: "install the compiler" },
{ command: "npx tsc --init", purpose: "create tsconfig.json" },
{ command: "npx tsc", purpose: "compile src/*.ts into dist/*.js" },
{ command: "node dist/app.js", purpose: "run the compiled JavaScript" },
];
steps.forEach((step, index) => {
console.log(`${index + 1}. ${step.command} -> ${step.purpose}`);
});Code breakdown
- Describe the data
type SetupStep = { command: string; purpose: string };Each step must have two string fields, `command` and `purpose`. You will study `type` in detail in later lessons.
- A typed array
const steps: SetupStep[] = [`SetupStep[]` means 'an array of SetupStep'. If any element misses `purpose` or misspells it, `npx tsc` reports an error.
- Loop and print
steps.forEach((step, index) => {TypeScript knows `step` is a `SetupStep`, so typing `step.` suggests `command` and `purpose`. `index` starts at 0, which is why the line prints `index + 1`.
1. npm install --save-dev typescript -> install the compiler
2. npx tsc --init -> create tsconfig.json
3. npx tsc -> compile src/*.ts into dist/*.js
4. node dist/app.js -> run the compiled JavaScript- Read tsconfig.json
- Collect the .ts files in src/
- Type-check them
- Write .js files into dist/
No errors
Exit code 0; dist/app.js is ready to run
Errors found
Errors are printed, exit code is non-zero, JS is still written
A faster feedback loop
Running two commands after every edit gets tiring. Scripts in package.json give the commands short names such as `npm run build`, and everyone on the team uses the same ones.
| Command | What it does |
|---|---|
| npx tsc --watch | Recompiles automatically whenever a file changes |
| npx tsc --noEmit | Type-checks only and writes no files (good for CI) |
| npx tsx src/app.ts | Strips types and runs directly, without type-checking |
To use `tsx`, install it first with `npm install --save-dev tsx`. It is fast but skips type checking, so remember to run `npx tsc --noEmit` separately. If you would rather install nothing, the TypeScript Playground at typescriptlang.org/play shows the generated JavaScript and any errors next to your code as you type. Most examples in this course can be pasted there and tried.
In real projects
Setup in real projects
In real projects TypeScript is always a devDependency, and package.json has scripts such as `"build": "tsc"` and `"typecheck": "tsc --noEmit"`. On every pull request, the CI pipeline runs `npm ci` to install the exact versions from the lockfile, then `npm run typecheck`; a type error blocks the merge.
Frameworks such as Vite and Next.js generate a TypeScript setup for new projects, but underneath it is made of the same parts as this lesson: `typescript` in devDependencies, a tsconfig.json, and a tool that produces JavaScript. Once you understand those parts, you can fix a framework's setup yourself when something goes wrong, instead of searching for a magic config line to copy.
Common mistakes
Common setup mistakes
Running `tsc app.ts` with a file name
As soon as a file name is given, tsconfig.json is not read at all, so your strict checks are off and the output lands next to the source instead of in `dist/`. Inside a project, always run `npx tsc` with no file name.
Assuming errors stop JavaScript from being written
By default `tsc` prints the type errors and exits with code 2, but it has already written the `.js` files. If you miss the errors, you may run buggy code. To get no output while errors exist, add `"noEmitOnError": true` to tsconfig.json.
Editing files in `dist/`
Changes to `dist/app.js` vanish the next time `npx tsc` runs. Always make your edits in the `.ts` files in `src/`.
Mini challenge
Mini challenge: a Node.js version checker
Write `src/check-node.ts` with typed functions that take a version string such as `"v22.11.0"` and check whether its major version meets a minimum. Compile it with `npx tsc` and run `node dist/check-node.js`.
- `majorVersion(version: string): number` strips the leading `v` and returns the first number
- Write `isSupported(version: string, minimumMajor: number): boolean`
- Check `v22.11.0` and `v16.20.2` against a minimum of 18 and print the results
function majorVersion(version: string): number {
// TODO: drop a leading "v", split on ".", convert the first part to a number
}
function isSupported(version: string, minimumMajor: number): boolean {
// TODO: compare the major version with minimumMajor
}
// TODO: loop over ["v22.11.0", "v16.20.2"] and print "<version> supported: <true|false>"Expected: Prints `v22.11.0 supported: true` and `v16.20.2 supported: false`.
Show hint
Combine `version.startsWith("v")`, `slice(1)`, `split(".")` and `Number(...)`.
Show solution
function majorVersion(version: string): number {
const withoutV = version.startsWith("v") ? version.slice(1) : version;
return Number(withoutV.split(".")[0]);
}
function isSupported(version: string, minimumMajor: number): boolean {
return majorVersion(version) >= minimumMajor;
}
for (const version of ["v22.11.0", "v16.20.2"]) {
console.log(`${version} supported: ${isSupported(version, 18)}`);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- With Node.js and npm installed, add the compiler to the project with `npm install --save-dev typescript`
- Create tsconfig.json with `npx tsc --init` and enable `rootDir: ./src` and `outDir: ./dist`
- Compile with `npx tsc` (no file name) and run the output with `node dist/app.js`
- By default JS is written even when there are errors; `noEmitOnError` changes that
- Use the TypeScript Playground to experiment without installing anything
Announcing TypeScript 7.0 — TypeScript