Skip to content

Installing TypeScript and the Compiler

TypeScript is installed as a Node.js package. Once it is installed, the tsc command compiles your .ts source files into .js files the browser can run.

TypeScript requires Node.js. Go to nodejs.org and download the LTS release for your operating system. Run the installer and accept the defaults.

Verify the installation:

Terminal window
node --version
npm --version

Both commands should print a version number. npm is Node’s package manager — it is included automatically with the Node.js installer and is what you will use to install TypeScript.

TypeScript is installed as a dev dependency — it is only needed during development, not at runtime:

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

npm init -y creates a package.json with default values. npm install --save-dev typescript installs the TypeScript compiler and records it as a dev dependency.

After installation your package.json will include:

{
"devDependencies": {
"typescript": "^6.0.3"
}
}

The TypeScript compiler is available via npx:

Terminal window
npx tsc --version

You should see something like Version 6.0.3. This confirms the compiler is installed and accessible.

Add two scripts to package.json so you do not have to type npx tsc manually:

{
"scripts": {
"build": "tsc",
"watch": "tsc --watch"
}
}

npm run build compiles once. npm run watch recompiles automatically every time you save a .ts file.

Create a file called hello.ts:

const greeting: string = 'Hello from TypeScript';
console.log(greeting);

Compile it:

Terminal window
npx tsc hello.ts

This produces hello.js in the same directory:

const greeting = 'Hello from TypeScript';
console.log(greeting);

The type annotation (: string) is gone. The output is plain JavaScript. Open the file and compare — this is what the browser actually runs.

Add a type error to hello.ts:

const greeting: string = 42; // Error: Type 'number' is not assignable to type 'string'
console.log(greeting);

Run npx tsc hello.ts again. The compiler prints the error and does not produce a .js file. This is the core value of TypeScript — the error is caught before any code runs.

Fix the error and recompile to confirm it resolves.

You can also install TypeScript globally with npm install -g typescript, which makes tsc available as a direct command anywhere on your system. For course projects, a local install (per-project) is preferred — it ensures every project uses the exact version specified in package.json, and it works without any global configuration.

  1. Create a new folder called aceit/. Run npm init -y inside it.
  2. Install TypeScript as a dev dependency.
  3. Add build and watch scripts to package.json.
  4. Create hello.ts with the greeting example above and compile it.
  5. Open hello.js and confirm the type annotation is absent.
  6. Introduce a deliberate type error (e.g., assign 42 to a string variable) and run tsc again. Read the error message.
  • TypeScript is installed per-project with npm install --save-dev typescript.
  • The tsc command compiles .ts files to .js files. Run it with npx tsc or via a package.json script.
  • Type annotations are stripped from the output — the compiled JavaScript is plain, standard code.
  • Compiler errors prevent output from being generated, catching mistakes before code runs.
  • Local installation (dev dependency) is preferred over global for reproducibility.