In this segment, we will explore the installation process of TypeScript, the necessary prerequisites prior to installation, and various methods to install TypeScript.
Requirements
- Text Editor or IDE - VS Code (recommended), WebStorm, Sublime Text, etc.
- Node.js - Includes npm (Node Package Manager)
- TypeScript Compiler (tsc)
Methods for Installing TypeScript
TypeScript can primarily be installed in two ways:
- Utilize npm to install TypeScript (recommended)
- Add the TypeScript plug-in to your IDE
---
Installing TypeScript with npm
Step 1: Install Node.js
Obtain and set up Node.js by visiting the official site: https://nodejs.org
- For Windows/macOS: Acquire the installer and execute it
- For Linux: Utilize your package manager or nvm
Verify the installation:
node -v
npm -v
Step 2: Install TypeScript
Launch your terminal and execute one of the commands below:
# Install globally (recommended for beginners)
npm install -g typescript
# Install as dev dependency in a project
npm install typescript --save-dev
# Install latest version
npm install typescript@latest -g
Step 3: Verify Installation
tsc --version
You ought to observe the version number of TypeScript (for instance, Version 5.x.x).
---
Installing TypeScript in VS Code (Recommended)
Step 1: Install VS Code
Download Visual Studio Code from: https://code.visualstudio.com
Step 2: Install the TypeScript Extension (Optional)
VS Code has built-in TypeScript support, but you can enhance it:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search for "TypeScript"
- Install official extensions if needed
Step 3: Create a TypeScript File
Generate a file with the .ts extension and begin programming!
// hello.ts
let message: string = "Hello, TypeScript!";
console.log(message);
Step 4: Compile and Run
# Compile TypeScript to JavaScript
tsc hello.ts
# Run the generated JavaScript
node hello.js
---
TypeScript Playground Online
Additionally, you can engage in TypeScript practice online without the need for installation by utilizing the official TypeScript Playground available at https://www.typescriptlang.org/play.