Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

Installing TypeScript and tsc

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.

The shortcut

Think "install, init, compile" — three commands, always in that order for a new project:

Setting up the task trackerStep 1 of 4
  1. Install

    npm install --save-dev typescript adds the compiler and records its version in package.json.

If two machines disagree about errors, check npx tsc --version on both first. A bare tsc may be running a global copy of a different version.

Drills

Answer fast — trust the rule.

quick drill

Why is TypeScript usually installed with --save-dev?

quick drill

Which command creates a starter tsconfig.json?

quick drill

What does npx tsc do when it finds type errors, with default settings?

quick drill

You run npx tsc src/tasks.ts and your strict settings seem to be ignored. Why?

Remember

  • Install TypeScript per project as a dev dependency; tsc turns .ts into .js.
  • Install, init, compile: npm install --save-dev typescript, npx tsc --init, npx tsc.
  • Trap: tsc file.ts skips tsconfig.json, and a bare tsc may be a different global version.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSetting up a fresh project

Describe the commands you would run to add TypeScript to a new Node.js project, and explain why installing it per project is better than relying only on a global install.