runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript Installation and Setup: Complete Beginner Guide

Learn how to install TypeScript, set up Node.js, create a TypeScript project, compile TypeScript files, and prepare a beginner-friendly development workflow.

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:

ToolPurpose
Node.jsRuns JavaScript outside the browser
npmInstalls TypeScript and other packages
TypeScript compilerConverts .ts files into .js files
Code editorHelps 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.org

After installation, open your terminal and check:

node -v
npm -v

You should see version numbers.

Example:

v22.0.0
10.0.0

If 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-practice

Initialize a Node project:

npm init -y

This 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-dev

Why --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 -v

tsc means TypeScript Compiler.

npx lets you run the local TypeScript compiler installed in your project.

You should see something like:

Version 5.x.x

Step 5: Create Your First TypeScript File

Create a file named:

index.ts

Add 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: string
  • course: string
  • function parameters with types
  • function return type

Step 6: Compile TypeScript to JavaScript

Run:

npx tsc index.ts

This creates:

index.js

Open 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.js

Output:

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 typescript

This installs TypeScript globally.

But for real projects, local installation is better:

npm install typescript --save-dev
InstallationCommandBest For
Localnpm install typescript --save-devReal projects
Globalnpm install -g typescriptQuick 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 start

This 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 -v

If it still fails, reinstall Node.js and open a new terminal.

Error: tsc is not recognized

Use:

npx tsc -v

If that fails, install TypeScript:

npm install typescript --save-dev

Error: Cannot find package

Run installation again inside your project folder:

npm install

Recommended 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.json

But 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 .ts file
  • 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.