Skip to the workspace
Pebellslearn by building
Introduction to TypeScript  ›  Lesson

Installing TypeScript and tsc

Installation and Configuration 12 minTypeScript is installed per project as an npm dev dependency, and its compiler tsc turns .ts files into .js files using settings from tsconfig.json.

typescript / Installing TypeScript and tsc

Explore each scene at your own pace.

01 · The rule

The rule

Installing TypeScript and tsc

TypeScript is installed per project as an npm dev dependency, and its compiler tsc turns .ts files into .js files using settings from tsconfig.json.

Install a project compiler and verify which version the project actually invokes.

You clone a teammate's project, type tsc, and get a different error list from theirs. The reason is usually that you are running a different compiler version. Installing TypeScript the right way — inside the project — makes every machine agree.

The rule

Install TypeScript per project; tsc turns .ts into .js.

TypeScript is an npm package, and its compiler is a command-line program called tsc (TypeScript compiler). Add it to a project as a development dependency, because it is needed to build the code, not to run it:

npm init -y
npm install --save-dev typescript

Now the compiler lives in node_modules, and its exact version is written into package.json. Run it through npx, which finds the project's local copy:

npx tsc --version     # which compiler this project uses
npx tsc --init        # create a starter tsconfig.json
npx tsc               # compile the project using tsconfig.json

Suppose src/tasks.ts contains:

const title: string = "Buy milk";
console.log(title.toUpperCase());

Running npx tsc checks it and writes a matching .js file (where it goes depends on the settings in tsconfig.json, which gets its own lesson). If there is a type error, tsc prints it with the file name and line number — and by default it still writes the JavaScript, because the output is plain JavaScript either way.

You can also compile one file directly with npx tsc src/tasks.ts. Be aware that naming files on the command line makes tsc ignore tsconfig.json and use default settings, which surprises many people.

A global install (npm install -g typescript) works for quick experiments, but the project copy is the one that counts.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.