Start using TypeScript

Install TypeScript in a project

Add the compiler as a development dependency and run the project-local version so every contributor uses the same toolchain.

TypeScript is an npm package. You install it in the project, like any other dependency. My advice is to never rely on a global install, and I’ll explain why in a moment.

Create a folder for the course, initialize a package.json, and install the compiler:

mkdir typescript-course
cd typescript-course
npm init -y
npm install --save-dev typescript

Now check that the project-local compiler works:

npx tsc --version

You should see something like this:

Version 5.9.3

tsc is the TypeScript compiler. npx looks for the tsc executable inside the project’s node_modules folder and runs it. You never need to type the full path to node_modules/.bin/tsc.

Why a local install

Open package.json. You will find a new entry:

"devDependencies": {
  "typescript": "^5.9.3"
}

This line is the point. The compiler version is now recorded in the project. Anyone who clones the repository and runs npm install gets the same compiler you have.

That matters more than it looks. Compiler versions add new checks and understand new syntax. Two people should not get different errors from the same code because their global tools differ. With a local install, the project decides the version, not the machine.

The --save-dev flag puts TypeScript under devDependencies instead of dependencies. That is the right place. Your application does not need the compiler once you have produced runnable JavaScript. The only exception is when your deployment process compiles on the server, and even then the dev dependency gets installed during the build.

When npx tsc fails

If npx tsc prints an error instead of a version, the install did not finish or you are in the wrong folder. Check that package.json lists typescript under devDependencies and that a node_modules/typescript folder exists. Run npm install again if it does not.

Be careful with the quick fix that comes to mind here: npm install -g typescript. It hides the real problem and brings back the version mismatch we just avoided. Fix the local project instead.

Try this on your own: run npx tsc --version, then open package.json and find the recorded TypeScript range. The ^ in front of the version means npm may install newer patch and minor releases, but never a new major version.

Lesson completed