Skip to content

TypeScript - Environment Setup

To start developing with TypeScript, you need a few essential tools installed on your system: a code editor, Node.js (which includes npm), and the TypeScript compiler itself.

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. It’s essential for running the TypeScript compiler and many other development tools.

npm (Node Package Manager) is included with Node.js. It’s used to install and manage project dependencies, including the TypeScript compiler and external libraries.

  1. Download Node.js: Go to the official Node.js website: https://nodejs.org/
  2. Choose Installer: Download the installer recommended for most users (LTS - Long Term Support version is usually a stable choice). Installers are available for Windows, macOS, and Linux.
  3. Run Installer: Follow the installation wizard instructions.
  4. Verify Installation: Open your terminal or command prompt and type the following commands to check if Node.js and npm were installed correctly:
  5. node -v (Should display the Node.js version)
  6. npm -v (Should display the npm version)
# Example verification output (versions may differ)
$ node -v
v18.16.0
$ npm -v
9.5.1

The TypeScript compiler, named tsc, translates your .ts files into .js files. You install it using npm.

You can install it globally (recommended for easy command-line access anywhere) or locally per project (standard practice for collaborative projects to ensure consistent versions).

Run the following command in your terminal:

npm install -g typescript

(On macOS/Linux, you might need sudo npm install -g typescript if you encounter permission errors).

Verify the installation:

tsc -v

This should display the installed TypeScript version.

Section titled “Local Installation (Per Project - Recommended for Projects)”
  1. Navigate to your project’s root directory in the terminal.
  2. Initialize your project for npm if you haven’t already: npm init -y (This creates a package.json file).
  3. Install TypeScript as a development dependency: npm install --save-dev typescript

When installed locally, you can run the compiler using npx tsc or by adding scripts to your package.json file.

You can write TypeScript in any text editor, but using an IDE with good TypeScript support significantly enhances the development experience.

VS Code (https://code.visualstudio.com/) is a free, popular, and powerful code editor developed by Microsoft with excellent built-in TypeScript support, including:

  • IntelliSense (autocompletion, parameter info)
  • Real-time error checking
  • Debugging support
  • Code navigation and refactoring
  • Git integration

Download and install VS Code for your operating system.

Other editors with strong TypeScript support include:

  • WebStorm (Paid)
  • Sublime Text (with TypeScript plugins)
  • Vim/Neovim (with language server configurations)
  • Atom (with TypeScript plugins)
  1. Write Code: Create files with a .ts extension in your chosen editor.
  2. Configure (Optional but Recommended): Create a tsconfig.json file (tsc --init) to configure compiler options.
  3. Compile: Run tsc (if using tsconfig.json) or tsc yourfile.ts in the terminal to generate JavaScript files.
  4. Run: Execute the generated .js file using Node.js (node yourfile.js) or include it in an HTML file for browser execution.
  5. Development Shortcut: Use ts-node yourfile.ts (requires npm install -g ts-node or local install) to compile and execute TypeScript directly during development.

For quick experiments without local setup, use the official online TypeScript Playground: https://www.typescriptlang.org/play

The Playground lets you write TypeScript code, see the generated JavaScript in real-time, and check for errors.

// Try this in the Playground:
function greetUser(user: { name: string, age: number }): string {
return `Hello, ${user.name}! You are ${user.age}.`;
}
const person = { name: "Alice", age: 30 };
console.log(greetUser(person));

With these tools installed and configured, you’re ready to start writing and compiling TypeScript code!