TypeScript
Introduction to TypeScript
TypeScript is a superset of JavaScript that adds static typing. You write JavaScript with type annotations, and the TypeScript compiler checks your code for type errors before it runs — then compiles to plain JavaScript that runs anywhere.
// JavaScript — bug only found at runtime
function greet(name) {
return "Hello, " + name.toUpperCase();
}
greet(42); // 💥 runtime crash: name.toUpperCase is not a function
// TypeScript — error caught at compile time
function greet(name: string) {
return "Hello, " + name.toUpperCase();
}
greet(42); // ❌ Argument of type 'number' is not assignable to 'string'
Why use it?
- Catch bugs early — type errors, typos, and null issues surface before running.
- Better tooling — autocomplete, inline documentation, safe refactoring.
- Self-documenting — types describe what functions expect and return.
- Scales — invaluable in large codebases and teams.
💡
All valid JavaScript is valid TypeScript. You can adopt it gradually, file by file.
Basic type annotations
let title: string = "Hello";
let count: number = 42;
let isActive: boolean = true;
let tags: string[] = ["a", "b"];
let pair: [string, number] = ["age", 30]; // tuple
// Functions
function add(a: number, b: number): number {
return a + b;
}
// Often you can let TypeScript infer the type
let inferred = "TypeScript knows this is a string";
💡
You don't need to annotate everything. TypeScript infers types from values — annotate function parameters and let inference handle the rest where it's clear.
Watch & Learn
A recommended video to watch alongside this chapter.
More “Introduction to TypeScript” videos on YouTube