Language Guide
What Is TypeScript?
TypeScript is JavaScript with types. It catches bugs before you run your code, gives you better editor autocomplete, and is now the standard for building serious JavaScript apps.
Quick answer: TypeScript is a superset of JavaScript that adds static types. You write .ts files, compile them to .js, and run the compiled output anywhere JavaScript runs. It's free, open source, and maintained by Microsoft.
💡 Why TypeScript Matters
- Catches errors early. Typos, wrong argument types, missing properties — all caught before the code runs.
- Better autocomplete. Your editor knows the shape of every object and suggests only valid properties.
- Self-documenting. Type signatures tell other developers (and future you) exactly what a function expects.
- Refactor with confidence. Rename a property — the compiler finds every place it's used.
- Industry standard. Most modern frameworks (Next.js, NestJS, Angular) and codebases use TypeScript by default.
⚖️ TypeScript vs JavaScript
Here's the same function in both languages:
JavaScript:
function greet(name) {
return "Hello, " + name;
}
greet("World"); // "Hello, World"
greet(42); // "Hello, 42" - no error
TypeScript:
function greet(name: string): string {
return "Hello, " + name;
}
greet("World"); // "Hello, World"
greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'
TypeScript catches the second call at compile time — before it ever reaches a user.
🧩 Basic TypeScript Types
// Primitives
let name: string = "Sarah";
let age: number = 25;
let isActive: boolean = true;
// Arrays
let scores: number[] = [90, 85, 100];
let tags: string[] = ["javascript", "typescript"];
// Objects with a defined shape
let user: { name: string; age: number } = {
name: "Sarah",
age: 25
};
// Functions with typed parameters and return value
function add(a: number, b: number): number {
return a + b;
}
// Union types (either A or B)
let id: string | number = "abc123";
id = 42; // also valid
// Optional properties
function log(message: string, userId?: number) {
console.log(message, userId);
}
Install TypeScript
TypeScript is a Node.js package. Install it globally via npm:
npm install -g typescript
Verify the installation:
tsc --version
You should see something like Version 5.5.3.
💡 Need Node.js first? See our Install Node.js guide.
Write and Compile Your First File
Create a file called hello.ts:
let message: string = "Hello, TypeScript!";
console.log(message);
Compile it to JavaScript:
tsc hello.ts
This creates hello.js next to your .ts file. Now run it:
node hello.js
You should see Hello, TypeScript!
See TypeScript Catch a Bug
Edit hello.ts to add a type error:
let message: string = "Hello, TypeScript!";
let count: number = "five"; // Error!
console.log(message);
Compile again:
tsc hello.ts
TypeScript refuses to compile and shows:
hello.ts:2:5 - error TS2322: Type 'string' is not assignable to type 'number'.
That's the whole point — you caught the bug before running the code. 🎉
🚫 Common Beginner Mistakes
- Using
anyeverywhere. It defeats the purpose. Use it sparingly and only when you truly don't know the type. - Forgetting that browsers don't run .ts files. You always need to compile to .js first.
- Assuming TypeScript is a different language. It's JavaScript plus types — all valid JS is valid TS.
- Not enabling
strictmode. Turn onstrict: truein tsconfig.json for the full benefit. - Skipping JavaScript fundamentals. Learn JS first. TypeScript adds to it, not replaces it.
❓ Frequently Asked Questions
What is TypeScript?
A superset of JavaScript that adds static types. It compiles to plain JavaScript and runs anywhere JS runs.
Is TypeScript better than JavaScript?
It catches more bugs and scales better for large projects. For quick scripts, plain JS is fine. For anything serious, TypeScript is now the standard.
Do I need to learn JavaScript first?
Yes. TypeScript is JavaScript + types. Learn JS fundamentals first — variables, functions, arrays, objects, async/await.
Does TypeScript run in the browser?
No. Browsers only understand JavaScript. You compile .ts to .js and run the .js output.
Is TypeScript free?
Yes. Open source, maintained by Microsoft, free for personal and commercial use.