0
0
Typescriptprogramming~5 mins

TypeScript Compiler Installation and Setup

Choose your learning style9 modes available
Introduction

We install the TypeScript compiler to turn TypeScript code into JavaScript that browsers can understand.

When you want to write safer JavaScript with types.
When starting a new project using TypeScript.
When you need to convert TypeScript files to JavaScript to run them.
When setting up your development environment for TypeScript coding.
Syntax
Typescript
npm install -g typescript

# To check the installed version
tsc --version

# To compile a TypeScript file
tsc filename.ts

npm install -g typescript installs the TypeScript compiler globally on your computer.

tsc is the command to run the TypeScript compiler.

Examples
This command installs the TypeScript compiler globally so you can use it anywhere on your computer.
Typescript
npm install -g typescript
This command shows the version of the TypeScript compiler installed.
Typescript
tsc --version
This command compiles the app.ts TypeScript file into JavaScript.
Typescript
tsc app.ts
Sample Program

This example shows how to install TypeScript, write a simple TypeScript file, compile it to JavaScript, and run it.

Typescript
/* Step 1: Install TypeScript globally (run in terminal)
npm install -g typescript

/* Step 2: Create a file named hello.ts with this content: */
console.log('Hello from TypeScript!');

/* Step 3: Compile the TypeScript file (run in terminal)
tsc hello.ts

/* Step 4: Run the compiled JavaScript file (run in terminal)
node hello.js
OutputSuccess
Important Notes

Make sure you have Node.js and npm installed before installing TypeScript.

You can also install TypeScript locally in a project without the -g flag.

Use tsc --watch to automatically compile files when you save changes.

Summary

Install TypeScript compiler using npm install -g typescript.

Use tsc filename.ts to compile TypeScript files to JavaScript.

Check your installation with tsc --version.