TypeScript adds type checking to JavaScript. Think of it as a spell checker - catches mistakes before you publish.
Writing without spell check, you might publish typos. TypeScript is like a spell checker for code - it catches type errors (like passing a number where text is expected) before your code runs.
JavaScript vs TypeScript
JavaScript:
- Dynamic typing - errors at runtime
- No type checking
- Flexible but error-prone
TypeScript:
- Static typing - errors before running
- Type checking during development
- Catches bugs early
Type Examples
let name: string = "Alice";
let age: number = 25;
let isActive: boolean = true;
let items: string[] = ["a", "b", "c"];
Benefits
- 🐛 Catch Errors Early: Before code runs
- 💡 Better IDE Support: Autocomplete, hints
- 📖 Self-Documenting: Types explain code
- 🔄 Refactoring: Safer code changes
- 👥 Team Collaboration: Clear contracts
Common Types
- 📝 string: Text data
- 🔢 number: Numeric values
- ✅ boolean: true/false
- 📦 object: Complex data
- 📚 array: Lists
- ❓ any: Any type (avoid when possible)
Interfaces
interface User {
name: string;
age: number;
email?: string; // Optional
}
Real-World Usage
- 🅰️ Angular: Built with TypeScript
- ⚛️ React: Popular with TypeScript
- 💚 Vue: TypeScript support
- 🟢 Node.js: Backend with TypeScript
The bottom line: TypeScript catches type errors before runtime. Like a spell checker for code, it helps you write more reliable JavaScript!