Typescript Foundations / Introduction to Typescript

Installation

1️⃣ Make sure Node.js is installed

TypeScript runs using Node.js.

👉 Check Node version:

node -v

👉 If not installed, download Node.js.


2️⃣ Install TypeScript globally

This allows you to use the TypeScript compiler (tsc) from anywhere.

npm install -g typescript

👉 Check if TypeScript is installed:

tsc -v

This will print the installed TypeScript version, e.g., Version 5.4.0.


3️⃣ (Recommended) Install TypeScript locally in a project

It’s best practice to install TypeScript per project so your team uses the same version:

npm init -y         # if you don’t have a package.json yet
npm install typescript --save-dev

👉 Add a script to your package.json:

"scripts": {
  "build": "tsc"
}

Now you can run:

npm run build

4️⃣ Initialize TypeScript config

Create a tsconfig.json to configure the TypeScript compiler:

tsc --init

👉 This generates a default tsconfig.json.

👉 You can edit it (e.g., enable strict: true).


5️⃣ Write your first TypeScript file

Create a file index.ts:

const greet = (name: string) => {
  console.log(`Hello, ${name}`);
};
greet("TypeScript");

Compile it:

tsc

Run the generated JS:

node index.js

📝 Summary commands

npm install -g typescript
tsc -v
tsc --init
npm install typescript --save-dev
tsc  # compile .ts files
node yourFile.js

🚀 Bonus

👉 If you want live compilation while coding:

tsc --watch

👉 If you want TypeScript in a React app:

npx create-react-app my-app --template typescript