Master Code
On The Go

Learn. Practice. Build.

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

⚖️ 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);
}
1

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.

2

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!

3

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

❓ 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.

🎯 What's Next?

← All How-To Guides