Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Environment Setup

Learn how to prepare your computer for TypeScript development, create a project folder, configure your editor, and compile your first TypeScript program.

Simple Explanation: An environment is the collection of tools you need to write, check, compile, and run your code. We will prepare these tools step by step.

What Do You Need?

Tool Purpose
Node.js Runs JavaScript outside the browser.
npm Installs TypeScript and other packages.
Visual Studio Code Helps you write and manage source code.
TypeScript Compiler Checks TypeScript code and generates JavaScript.
Important: Node.js normally includes npm. Install Node.js before trying to install TypeScript through npm.

TypeScript Workflow Diagram

Follow the diagram to understand how TypeScript code moves from writing the program to running it.

💻
Step 1
Write Code

Create your file in the editor.

app.ts
→
🔍
Step 2
Check and Compile

The TypeScript compiler checks types and generates JavaScript.

tsc
→
📄
Step 3
Generate JavaScript

The compiler produces JavaScript output.

app.js
→
🚀
Step 4
Run the Program

Execute JavaScript in a compatible runtime.

Node.js
Remember: Node.js and npm help prepare the development environment. The TypeScript compiler converts TypeScript code into JavaScript; Node.js can then execute the generated JavaScript.

1. Create a Project Folder

Create a dedicated folder for your TypeScript practice. Open Command Prompt or a terminal and run these commands.

Example Commands

mkdir TypeScriptDemo
cd TypeScriptDemo

Folder Structure

TypeScriptDemo/
    app.ts
    app.js

The TypeScript source file is named app.ts. The JavaScript file app.js is normally generated after compilation.

2. Set Up Visual Studio Code

Visual Studio Code is an editor that provides syntax highlighting, code navigation, and TypeScript language support.

  1. Open Visual Studio Code.
  2. Select File → Open Folder.
  3. Select your TypeScriptDemo folder.
  4. Create a new file called app.ts.
  5. Write your TypeScript code and save the file.
Tip: TypeScript language support is built into Visual Studio Code. The TypeScript compiler is still needed for the compilation workflow used in this lesson.

3. Create Your First TypeScript Program

Open app.ts and enter the following code.

Example: app.ts

let studentName: string = "Rahul";
let age: number = 22;

console.log("Student Name: " + studentName);
console.log("Age: " + age);

Expected Output

Student Name: Rahul
Age: 22

The variable studentName is a string, while age is a number.

4. Compile TypeScript

Run the compiler from the folder containing app.ts.

Command

npx tsc app.ts

Result

app.ts
app.js

If TypeScript is installed as a project dependency and compilation succeeds, the compiler normally creates app.js.

5. Execute the Generated JavaScript

Use Node.js to execute the generated JavaScript file.

Command

node app.js

Expected Output

Student Name: Rahul
Age: 22
Congratulations! You have created, compiled, and executed your first TypeScript program.

Environment Setup Checklist

Before moving to the next lesson, verify that you can complete each task.

Practice Goal: Try completing all five tasks before moving to the next lesson.

Common Mistakes

  • Writing code in the wrong folder.
  • Forgetting to save app.ts.
  • Running node app.js before generating the JavaScript file.
  • Confusing the .ts source file with the .js output file.
  • Trying to run npx tsc without installing TypeScript in the project or making the compiler available.

Summary

A TypeScript development environment includes a code editor, Node.js, npm, and the TypeScript compiler. After preparing the environment, create a TypeScript source file, compile it into JavaScript, and execute the generated file. The workflow diagram above summarises the process.