What is TypeScript?
TypeScript is a programming language created by Microsoft. It is a superset of JavaScript — meaning every valid JavaScript program is also a valid TypeScript program, but TypeScript adds powerful new features on top.
The biggest feature TypeScript adds is static type checking. This means you can declare what type of data a variable holds, and TypeScript will catch type-related bugs before your code ever runs.
Why TypeScript?
- → Catches bugs at compile time instead of runtime.
- → Better editor support — autocompletion, refactoring, and inline documentation.
- → Makes large codebases easier to understand and maintain.
- → TypeScript compiles down to plain JavaScript, so it runs anywhere JS runs.
TS vs JS
Think of TypeScript as JavaScript with a safety net. In plain JavaScript, you might accidentally assign a number to a variable that should be a string — and the error only shows up when the program runs. TypeScript tells you about that mistake the moment you save the file.
// JavaScript — this runs fine, but might cause a bug later
let name = "Alice";
name = 42; // No error!
// TypeScript — this catches the bug immediately
let name: string = "Alice";
name = 42; // Error: Type 'number' is not assignable to type 'string'
Basic types
TypeScript has several built-in types you'll use constantly:
let greeting: string = "Hello"; // Text
let count: number = 42; // Any number (int or float)
let isActive: boolean = true; // true or false
let items: string[] = ["a", "b"]; // An array of strings
Type inference
You don't always need to write types explicitly. TypeScript can often figure them out on its own: let count = 42 — TypeScript knows count is a number.
Quiz
In the editor above, declare a string variable with your name and log it to the console using console.log(). Try changing the type to number and see what happens.