Introduction to TypeScript
Introduction to TypeScript
Section titled “Introduction to TypeScript”What is TypeScript?
Section titled “What is TypeScript?”TypeScript is a superset of JavaScript developed by Microsoft that adds static type checking to the language. It compiles down to plain JavaScript that runs anywhere JavaScript runs — browsers, Node.js, Deno, and more.
Analogy: JavaScript is like writing a shopping list on a napkin — flexible but easy to make mistakes. TypeScript is like using a structured shopping app — it tells you when you’ve typed “mil” instead of “milk” before you go to the store.
Key Facts
Section titled “Key Facts”| Fact | Detail |
|---|---|
| Creator | Anders Hejlsberg (also created C#) |
| First Release | October 2012 |
| Current Version | TypeScript 5.x (2024+) |
| Paradigm | Multi-paradigm: OOP, Functional, Generic |
| Compilation | tsc compiles .ts to .js |
| License | Apache 2.0 |
Why TypeScript Was Created
Section titled “Why TypeScript Was Created”JavaScript was never designed for large-scale applications. As web apps grew from simple scripts to complex enterprise systems (Slack, VS Code, Figma), JavaScript’s lack of a type system became a major pain point:
- Runtime errors — Most bugs appear only at runtime
- Poor tooling — No autocomplete, refactoring, or go-to-definition
- Documentation gaps — Functions don’t declare what they expect or return
- Team scalability — Large codebases become unmaintainable
TypeScript solves these by catching errors at compile time, before your code ever runs.
Problems TypeScript Solves
Section titled “Problems TypeScript Solves”| Problem | JavaScript | TypeScript |
|---|---|---|
| Undefined is not a function | Detected at runtime | Detected at compile time |
| Wrong argument types | Silent failure | Compiler error |
| Refactoring a function signature | Manual search & replace | Compiler shows all call sites |
| Missing properties on objects | undefined at runtime | Compiler error |
| Team code reviews | Miss subtle type bugs | Compiler catches them |
TypeScript vs JavaScript
Section titled “TypeScript vs JavaScript”// JavaScript — no type checkingfunction greet(name) { return "Hello, " + name.toUpperCase();}greet(42); // Runtime Error: name.toUpperCase is not a function
// TypeScript — catches the bug at compile timefunction greet(name: string): string { return "Hello, " + name.toUpperCase();}greet(42); // ❌ Error: Argument of type 'number' is not assignable to parameter of type 'string'The Type System Flow
Section titled “The Type System Flow”flowchart TB TS[Your TypeScript Code<br/>.ts file] --> TSC[TypeScript Compiler<br/>tsc] TSC --> Check{Type Checking<br/>Passes?} Check -->|No ❌| Errors[Errors Reported<br/>Fix & Re-compile] Check -->|Yes ✅| Transpile[Transpile to JavaScript] Transpile --> JS[JavaScript Output<br/>.js file] JS --> Runtime[Execute in<br/>Browser / Node.js]
style TS fill:#7c3aed,color:#fff style TSC fill:#3b82f6,color:#fff style Check fill:#f59e0b,color:#fff style Errors fill:#ef4444,color:#fff style Transpile fill:#059669,color:#fff style JS fill:#7c3aed,color:#fff style Runtime fill:#10b981,color:#fffTypeScript adds a compile-time type checking layer between your code and runtime execution.
Real-World Usage
Section titled “Real-World Usage”Who uses TypeScript?
| Company | Usage |
|---|---|
| Microsoft | VS Code, Teams, Azure SDK |
| Angular, Firebase | |
| Airbnb | Full-stack web apps |
| Slack | Desktop and mobile apps |
| Asana | Web application |
| Shopify | Storefront and admin |
| Vercel | Next.js framework |
Common Mistakes
Section titled “Common Mistakes”| Mistake | Why It’s Wrong | Fix |
|---|---|---|
Using any everywhere | Disables type checking entirely | Use specific types or unknown |
Not configuring tsconfig.json | Misses strict checks | Use strict: true |
| Overusing type assertions | Forces the compiler to trust you | Use proper type guards |
Ignoring compiler errors with // @ts-ignore | Silences real bugs | Fix the underlying type issue |
Best Practices
Section titled “Best Practices”- Enable
strict: trueintsconfig.jsonfrom the start - Prefer
interfaceovertypefor object shapes (they extend better) - Avoid
any— useunknownwhen you don’t know the type - Use
constassertions (as const) for literal types - Export types from a central
types.tsfile - Use branded types for domain primitives (e.g.,
UserIdinstead ofstring) - Run
tsc --noEmitin CI to catch type errors
Interview Questions
Section titled “Interview Questions”Easy: What is the difference between TypeScript and JavaScript?
Medium: Explain how TypeScript’s compilation process works. What does tsc do?
Hard: How does TypeScript enable better refactoring in large codebases compared to JavaScript?
Learning Path
Section titled “Learning Path”flowchart TB JS[JavaScript Developer] --> Basic[Basic Types & Inference] Basic --> Func[Functions & Objects] Func --> Inter[Interfaces & Generics] Inter --> Adv[Utility & Advanced Types] Adv --> FW[React / Node / Angular Integration] FW --> Ent[Enterprise TypeScript 🚀]
style JS fill:#3b82f6,color:#fff style Basic fill:#7c3aed,color:#fff style Func fill:#059669,color:#fff style Inter fill:#f59e0b,color:#fff style Adv fill:#ec4899,color:#fff style FW fill:#06b6d4,color:#fff style Ent fill:#10b981,color:#fff