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.
Install Node.js
Section titled “Install Node.js”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:
node --versionnpm --versionBoth 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.
Install TypeScript in your project
Section titled “Install TypeScript in your project”TypeScript is installed as a dev dependency — it is only needed during development, not at runtime:
npm init -ynpm install --save-dev typescriptnpm 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" }}Run the compiler
Section titled “Run the compiler”The TypeScript compiler is available via npx:
npx tsc --versionYou should see something like Version 6.0.3. This confirms the compiler is installed and accessible.
Add build scripts
Section titled “Add build scripts”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.
Your first compile
Section titled “Your first compile”Create a file called hello.ts:
const greeting: string = 'Hello from TypeScript';console.log(greeting);Compile it:
npx tsc hello.tsThis 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.
Compiler errors
Section titled “Compiler errors”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.
Global vs local installation
Section titled “Global vs local installation”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.
Exercise
Section titled “Exercise”- Create a new folder called
aceit/. Runnpm init -yinside it. - Install TypeScript as a dev dependency.
- Add
buildandwatchscripts topackage.json. - Create
hello.tswith the greeting example above and compile it. - Open
hello.jsand confirm the type annotation is absent. - Introduce a deliberate type error (e.g., assign
42to astringvariable) and runtscagain. Read the error message.
- TypeScript is installed per-project with
npm install --save-dev typescript. - The
tsccommand compiles.tsfiles to.jsfiles. Run it withnpx tscor via apackage.jsonscript. - 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.