How to Install TypeScript

What you’ll build or solve

You’ll install TypeScript globally or locally in a project. By the end, the TypeScript compiler will be available from your terminal and ready to use.

When this approach works best

This method works best when:

  • You want to write and compile TypeScript locally on your machine.
  • You’re starting a new project that requires TypeScript.
  • You’re following a course that uses the tsc command.

Skip this if you’re using an online editor like StackBlitz or CodeSandbox that already includes TypeScript.

Prerequisites

  • Node.js installed
  • Access to a terminal

Check that Node.js is installed:

node-v

Check that npm is available:

Both commands should print a version number.


Step-by-step instructions

Step 1: Choose your installation method

Pick one of the options below.

Option A: Global installation (quick start)

This installs TypeScript system-wide.

npm install-g typescript

Verify it:

You should see something like:

Version 5.x.x

This allows you to run tsc from any folder.


Option B: Local installation (recommended for projects)

Use this if you’re working inside a specific project.

Create a project folder and move into it:

mkdir my-project
cd my-project

Initialize the project:

npm init-y

Install TypeScript as a development dependency:

npm install--save-dev typescript

Verify it:

npx tsc-v

npx runs the local version installed in your project.


Option C: Alternative package managers

If you use Yarn:

yarn add--dev typescript

If you use pnpm:

pnpm add-D typescript

Then verify with:

npx tsc-v

What to look for

  • tsc -v works for global installs
  • npx tsc -v works for local installs
  • If you see “command not found,” restart your terminal and try again

Installation is complete once the version command works.


Step 2: Verify the installation works

Create a test file called index.ts:

letmessage:string="Hello!";
console.log(message);

Now compile it.

If you installed globally:

tsc index.ts

If you installed locally:

npx tsc index.ts

This creates:

index.js

Run it with Node:

node index.js

If you see:

Hello!

TypeScript is installed correctly.


Examples you can copy

These examples help confirm your setup works.

Example 1: Simple number

letage:number=30;
console.log(age);

Compile:

tsc index.ts

or

npx tsc index.ts

Example 2: Function with types

functionmultiply(a:number,b:number):number {
returna*b;
}

console.log(multiply(4,5));

Compile and run:

tsc index.ts
node index.js

Example 3: Intentional type error

letscore:number="100";

Compile:

tsc index.ts

You should see a type error in the terminal. That confirms TypeScript is checking types correctly.


Common mistakes and how to fix them

Mistake 1: Running a .ts file directly with Node

What someone might do:

node index.ts

Why it breaks:

Node does not understand TypeScript syntax.

Correct approach:

tsc index.ts
node index.js

Or use npx tsc for local installs.


Mistake 2: tsc is not recognized

What someone might see:

tsc: command not found

Why it happens:

TypeScript is not installed globally, or your terminal session has not refreshed.

Fix:

  • Restart the terminal

Or use:

npx tsc-v

Mistake 3: Skipping npm init for local install

What someone might do:

npm install--save-dev typescript

Without creating a project first.

Why it causes issues:

No package.json exists, so the dependency is not tracked properly.

Fix:

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

Troubleshooting

If tsc -v fails but installation succeeded, restart your terminal.

If npx tsc fails, confirm you’re inside the correct project folder.

If permission errors appear during global install, try running your terminal as administrator, or use local installation instead.

If Node reports version conflicts, update to a recent LTS version of Node.js.


Quick recap

  • Install Node.js first.
  • Install TypeScript globally with npm install -g typescript, or locally with npm install --save-dev typescript.
  • Verify using tsc -v or npx tsc -v.
  • Compile a small test file to confirm everything works.
  • Run the compiled JavaScript with Node.