TypeScript Installation
Learn how to prepare your computer for TypeScript development, install the TypeScript compiler, and verify that the required commands work correctly.
Prerequisites
Before installing TypeScript, prepare the tools required for the development environment.
| Tool | Purpose | Verification Command |
|---|---|---|
| Node.js | Provides a JavaScript runtime. | node --version |
| npm | Installs and manages packages. | npm --version |
| Visual Studio Code | Provides an editor for writing TypeScript code. | Open the editor |
| TypeScript Compiler | Checks TypeScript code and generates JavaScript. | tsc --version |
1. Check Node.js Installation
Open Command Prompt or a terminal and execute the following command to check whether Node.js is available.
Example
node --version
Expected Output
v<installed Node.js version>
The exact version depends on the version installed on your computer.
2. Check npm Installation
npm is the package manager commonly used to install TypeScript and other JavaScript development tools.
Example
npm --version
Expected Output
<installed npm version>
If npm displays a version number, the command is available in your terminal.
3. Install TypeScript Globally
A global installation makes the TypeScript compiler command available from different folders, provided npm's global executable directory is configured in the system PATH.
Installation Command
npm install -g typescript
Verify the Installation
tsc --version
Expected Output
Version number of the installed TypeScript compiler
4. Install TypeScript in a Project
For project-based development, you can install TypeScript as a development dependency. This keeps the compiler dependency associated with the project.
Step 1: Create a Project Folder
mkdir TypeScriptDemo
cd TypeScriptDemo
Step 2: Create package.json
npm init -y
Step 3: Install TypeScript
npm install --save-dev typescript
Step 4: Check the Local Compiler
npx tsc --version
npx command can
run the compiler installed in the project.
5. Create Your First TypeScript File
Create a file named app.ts inside your
project folder. The .ts extension identifies
a TypeScript source file.
Example: app.ts
let instituteName: string = "CIIT Training Institute";
let courseName: string = "TypeScript";
console.log(instituteName);
console.log(courseName);
Compile the File
npx tsc app.ts
Generated File
app.ts
app.js
When compilation succeeds, the compiler normally produces
an app.js file next to the source file,
unless compiler settings specify another output location.
6. Execute the Generated JavaScript
After compiling the TypeScript source file, run the generated JavaScript using Node.js.
Command
node app.js
Expected Output
CIIT Training Institute
TypeScript
Global vs Local Installation
| Global Installation | Local Installation |
|---|---|
Installed using npm install -g typescript. |
Installed using npm install --save-dev typescript. |
| Compiler command is available globally when configured correctly. | Compiler dependency belongs to a particular project. |
| Useful for general learning and command-line practice. | Useful when a project needs a specific compiler dependency. |
Run the compiler using tsc. |
Run the local compiler using npx tsc. |
Common Installation Mistakes
- Installing TypeScript before Node.js and npm are available.
- Running terminal commands in the wrong project folder.
-
Using
tscwhen the global compiler is not installed or the executable directory is not in PATH. - Forgetting to run the compiler before trying to execute the generated JavaScript file.
-
Confusing the TypeScript source file
(
.ts) with the generated JavaScript file (.js). -
Forgetting that a local installation can be executed
with
npx tsc.
Summary
To install TypeScript, first verify that Node.js and npm
are available. Install the TypeScript compiler globally
or as a local project dependency, verify it with
tsc --version or npx tsc --version,
and compile a .ts file. The generated JavaScript
can then be executed using a compatible JavaScript runtime.