TypeScript Lesson 1: What is TypeScript?
TypeScript is JavaScript with types. Microsoft created it to catch errors before you run your code. It compiles down to regular JavaScript — browsers never see TypeScript directly.
The Core Idea
// JavaScript: no error detection until runtime
function greet(name) {
return "Hello, " + name.toUpperCase();
}
greet(42); // Runtime error: toUpperCase is not a function
// TypeScript: error caught BEFORE running
function greet(name: string): string {
return "Hello, " + name.toUpperCase();
}
greet(42); // ❌ Error: Argument of type "number" is not assignable to type "string"
Why TypeScript?
- Catch bugs early: Errors at compile time, not production
- Better autocomplete: Your editor knows what methods exist
- Self-documenting: Types explain what functions expect
- Refactoring: Rename things safely across the whole codebase
- Team scale: Enforces contracts between code written by different people
// TypeScript lets your editor help you
const user = { name: "Alice", age: 30 };
user. // editor shows: .name, .age — nothing else
// vs JavaScript where you get everything:
user. // editor shows hundreds of Object methods
// you have to guess what's valid
Who Uses TypeScript?
Google (Angular), Microsoft (VS Code), Airbnb, Slack, Asana, Discord — virtually every large JavaScript project now uses TypeScript.
🏋️ Practice Task
Read the TypeScript homepage (typescriptlang.org). Find the “Try” playground. Write this JS function in the playground and add types: function add(a, b) { return a + b; } Try calling add(“hello”, 5) — what error do you get?
💡 Hint: In the playground: function add(a: number, b: number): number { return a + b; } Then try add(“hello”, 5) and see the red underline.