Skip to main content
Advertisement

TypeScript: Installation and Getting Started

1. Install Node.js

To run and compile TypeScript, you must first have Node.js installed on your system. (Recommendation: LTS version).


2. Install TypeScript

Use the following command in your terminal or command prompt to install TypeScript globally or per project.

# Global installation (allows tsc to be used anywhere)
npm install -g typescript

# Verify the version
tsc -v

3. Create and Compile Your First File

Create a file named hello.ts and add the following code:

// hello.ts
const greet = (name: string) => {
return `Hello, ${name}!`;
};

console.log(greet("TypeScript"));

Compiling and Running

TypeScript cannot be executed directly by browsers or Node.js. It must be compiled into JavaScript first.

# 1. Compile (this generates hello.js)
tsc hello.ts

# 2. Run (using Node.js)
node hello.js

4. Using ts-node (Execute directly without manual compilation)

Manually compiling every time during development can be tedious. You can use ts-node to execute your TypeScript code directly.

npx ts-node hello.ts
Advertisement