Welcome back! I am Mihir, and in this lesson we will set up TypeScript on your system from scratch.
Before we write serious TypeScript code, we need a basic development environment. Do not worry if setup feels boring. Once this is done, your workflow becomes smooth: write .ts files, check errors, compile to JavaScript, and run the final code.
What We Need to Install
To use TypeScript locally, we need:
| Tool | Purpose |
|---|---|
| Node.js | Runs JavaScript outside the browser |
| npm | Installs TypeScript and other packages |
| TypeScript compiler | Converts .ts files into .js files |
| Code editor | Helps write and check code easily |
Most developers use VS Code because TypeScript support is excellent by default.
Step 1: Install Node.js
TypeScript projects commonly use Node.js tooling, even when the final app runs in the browser.
Go to the official Node.js website and install the LTS version:
https://nodejs.orgAfter installation, open your terminal and check:
node -v
npm -vYou should see version numbers.
Example:
v22.0.0
10.0.0If both commands work, Node.js and npm are ready.
Step 2: Create a TypeScript Project Folder
Create a new folder:
mkdir typescript-practice
cd typescript-practiceInitialize a Node project:
npm init -yThis creates a package.json file.
package.json stores project information, scripts, and installed packages.
Step 3: Install TypeScript
Install TypeScript as a development dependency:
npm install typescript --save-devWhy --save-dev?
Because TypeScript is mainly needed during development. The final output is JavaScript.
After installation, your package.json may include:
{
"devDependencies": {
"typescript": "^5.0.0"
}
}Step 4: Check TypeScript Version
Run:
npx tsc -vtsc means TypeScript Compiler.
npx lets you run the local TypeScript compiler installed in your project.
You should see something like:
Version 5.x.xStep 5: Create Your First TypeScript File
Create a file named:
index.tsAdd this code:
const mentor: string = "Mihir";
const course: string = "TypeScript";
function createMessage(name: string, topic: string): string {
return `Hello ${name}, welcome to ${topic}!`;
}
console.log(createMessage(mentor, course));This file uses TypeScript syntax:
mentor: stringcourse: string- function parameters with types
- function return type
Step 6: Compile TypeScript to JavaScript
Run:
npx tsc index.tsThis creates:
index.jsOpen index.js. You will see normal JavaScript:
var mentor = "Mihir";
var course = "TypeScript";
function createMessage(name, topic) {
return "Hello ".concat(name, ", welcome to ").concat(topic, "!");
}
console.log(createMessage(mentor, course));The TypeScript types are removed.
That is the key idea:
TypeScript is checked during development.
JavaScript runs in the actual environment.Step 7: Run the JavaScript File
Run:
node index.jsOutput:
Hello Mihir, welcome to TypeScript!Congratulations. You just compiled and ran TypeScript locally.
Local vs Global TypeScript Installation
You may see tutorials using:
npm install -g typescriptThis installs TypeScript globally.
But for real projects, local installation is better:
npm install typescript --save-dev| Installation | Command | Best For |
|---|---|---|
| Local | npm install typescript --save-dev | Real projects |
| Global | npm install -g typescript | Quick experiments |
Local installation keeps each project locked to its own TypeScript version.
Add Useful npm Scripts
Instead of typing npx tsc index.ts every time, you can add scripts in package.json.
{
"scripts": {
"build": "tsc index.ts",
"start": "node index.js"
}
}Now run:
npm run build
npm run startThis is cleaner and closer to professional workflow.
Common Setup Errors
Error: node is not recognized
Node.js is not installed correctly or your terminal needs to be restarted.
Try:
node -vIf it still fails, reinstall Node.js and open a new terminal.
Error: tsc is not recognized
Use:
npx tsc -vIf that fails, install TypeScript:
npm install typescript --save-devError: Cannot find package
Run installation again inside your project folder:
npm installRecommended Folder Structure
For now, keep it simple:
typescript-practice/
├── index.ts
├── index.js
├── package.json
├── package-lock.json
└── node_modules/Later, we will improve this using:
src/
dist/
tsconfig.jsonBut beginners should first understand the basic compile-and-run flow.
What You've Learned
You now know how to:
- Install Node.js
- Create a TypeScript project
- Install TypeScript locally
- Use
npx tsc - Create a
.tsfile - Compile TypeScript to JavaScript
- Run the compiled JavaScript with Node.js
- Understand local vs global TypeScript installation
What's Next?
In the next lesson, we will write your First TypeScript Program step by step and understand each line deeply.
Need Help?
- Have questions, confusion, or want to know more? Contact me
Setup is a one-time investment. Once your TypeScript environment is ready, learning becomes much more fun.