Chapter 2: TypeScript Getting Started
π TypeScript β Getting Started (Step-by-Step Guide for Beginners)
Now that you understand what TypeScript is, letβs learn how to start using TypeScript on your computer β like a real teacher explaining in class π
We will cover:
-
What you need before starting
-
Installing TypeScript
-
Writing your first TypeScript program
-
Compiling TypeScript
-
Using tsconfig.json
-
Running TypeScript in browser and Node.js
π 1οΈβ£ What You Need Before Starting
Since TypeScript converts into JavaScript, you need:
β 1. Node.js
TypeScript uses Node Package Manager (npm) to install.
Install:
π Node.js
Download from official website and install.
To check installation:
If version shows β installed successfully β
π 2οΈβ£ Installing TypeScript
After installing Node.js, open:
-
Command Prompt (Windows)
-
Terminal (Mac/Linux)
Then type:
This installs TypeScript globally.
To check:
If version appears like:
Then TypeScript installed successfully π
|
0 1 2 3 4 5 6 |
π 3οΈβ£ Writing Your First TypeScript Program |
Letβs create a project step by step.
π§βπ« Step 1: Create a Folder
Create a new folder:
Open this folder in VS Code (recommended editor).
π Visual Studio Code
π§βπ« Step 2: Create a TypeScript File
Create a file:
(.ts extension means TypeScript file)
π§βπ« Step 3: Write Code
Inside app.ts write:
π 4οΈβ£ Compile TypeScript to JavaScript
TypeScript cannot run directly.
You must compile it using:
Now you will see a new file:
The compiler converted TypeScript β JavaScript.
|
0 1 2 3 4 5 6 |
π Generated JavaScript File |
Now run it using Node:
Output:
π Congratulations! Your first TypeScript program works.
|
0 1 2 3 4 5 6 |
π 5οΈβ£ Understanding Compilation Error (Very Important) |
Letβs change code:
Now run:
You will get error like:
π This is the power of TypeScript.
It catches error BEFORE running program.
π 6οΈβ£ Using tsconfig.json (Professional Way)
When working on real projects, we donβt compile one file at a time.
We create a configuration file.
π§βπ« Step 1: Create tsconfig.json
Inside project folder, run:
This creates:
Example content:
What These Mean:
-
target β JavaScript version
-
module β module system
-
strict β strict type checking
-
outDir β output folder for JS files
|
0 1 2 3 4 5 6 |
π§βπ« Step 2: Compile Full Project |
Now just run:
It will compile all .ts files.
π 7οΈβ£ Running TypeScript with Node.js (Backend)
TypeScript is commonly used with:
π Node.js
Example:
Compile and run using Node.
|
0 1 2 3 4 5 6 |
π 8οΈβ£ Running TypeScript in Browser (Frontend) |
You cannot directly use .ts file in HTML.
Example:
index.html
π 9οΈβ£ Project Folder Structure (Best Practice)
Professional developers follow this structure.
π 1οΈβ£0οΈβ£ Optional: Using ts-node (Advanced Shortcut)
Instead of compiling manually, you can install:
Then directly run:
No need to manually compile.
π§ Important Concepts While Getting Started
When learning TypeScript, focus on:
-
Types (string, number, boolean)
-
Functions with types
-
Interfaces
-
Arrays
-
Objects
-
Strict mode
π― Small Real Project Example
Letβs build simple calculator.
Compile and run β Clean and error-free.
π Final Summary (Exam Ready Definition)
Getting Started with TypeScript means installing TypeScript using Node.js, creating .ts files, compiling them into JavaScript using the TypeScript compiler (tsc), and running the generated JavaScript file in browser or Node.js environment.
If you want, I can next explain:
-
πΉ TypeScript Project Setup with React
-
πΉ TypeScript Folder Structure for Large Projects
-
πΉ TypeScript vs JavaScript practical difference
-
πΉ TypeScript Interview Questions
-
πΉ Complete TypeScript syllabus roadmap
Just tell me π
