Why TypeScript & Setup

0/6 in this phase0/21 across the roadmap

📖 Concept

TypeScript is a statically-typed superset of JavaScript developed by Microsoft. Every valid JavaScript file is already valid TypeScript — TS adds an optional type layer that is erased at compile time, producing plain JavaScript.

Why TypeScript matters in production:

  • Catches bugs at compile time instead of runtime — null reference errors, typos, wrong argument types
  • Serves as living documentation — function signatures tell you exactly what goes in and comes out
  • Enables powerful IDE tooling — autocompletion, refactoring, jump-to-definition across large codebases
  • Scales to large teams — type contracts between modules prevent integration bugs

How TypeScript works internally:

  1. You write .ts files with type annotations
  2. The TypeScript compiler (tsc) type-checks the code
  3. Types are completely erased — the output is plain JavaScript
  4. No runtime overhead — TypeScript doesn't exist at runtime

Comparison with JavaScript: In JS, function add(a, b) { return a + b; } silently accepts add("hello", 5)"hello5". In TS, you declare function add(a: number, b: number): number and the compiler catches misuse before the code ever runs.

🏠 Real-world analogy: TypeScript is like spell-check for your code. The red squiggly lines catch mistakes as you type, but the final published document (JavaScript) doesn't contain any spell-check markup.

💻 Code Example

codeTap to expand ⛶
1// Install TypeScript
2// npm install -g typescript
3// npx tsc --init (creates tsconfig.json)
4
5// Basic type annotations
6let message: string = "Hello, TypeScript!";
7let count: number = 42;
8let isActive: boolean = true;
9
10// Type inference — TS figures out the type automatically
11let inferred = "TypeScript infers this as string"; // type: string
12let num = 100; // type: number
13
14// Function with type annotations
15function greet(name: string): string {
16 return `Hello, ${name}!`;
17}
18
19// greet(42); // ❌ Compile error: Argument of type 'number'
20// // is not assignable to parameter of type 'string'
21greet("Alice"); // ✅ Works
22
23// The compiled JavaScript output (types are erased):
24// function greet(name) {
25// return `Hello, ${name}!`;
26// }
27
28// tsconfig.json essentials (starter):
29// {
30// "compilerOptions": {
31// "target": "ES2022",
32// "module": "ESNext",
33// "strict": true, // Enable all strict checks
34// "noUncheckedIndexedAccess": true, // Arrays may be undefined
35// "esModuleInterop": true,
36// "outDir": "./dist",
37// "rootDir": "./src"
38// },
39// "include": ["src/**/*"]
40// }

🏋️ Practice Exercise

Mini Exercise:

  1. Install TypeScript globally and run tsc --init to create a tsconfig
  2. Create a .ts file that declares variables of each primitive type
  3. Write a function calculateArea(width: number, height: number): number
  4. Try passing wrong types — observe the compile-time errors
  5. Compile with tsc and inspect the generated JavaScript — notice types are gone

⚠️ Common Mistakes

  • Thinking TypeScript adds runtime overhead — types are completely erased at compile time; there is zero runtime cost

  • Using any everywhere to 'make it work' — this defeats the entire purpose of TypeScript

  • Not enabling strict: true in tsconfig — without it, many type checks are disabled and you lose most of TS's value

  • Confusing TypeScript with a different language — it's JavaScript with types, not a replacement. All JS knowledge applies directly

  • Thinking you need to annotate everything — TypeScript's type inference is powerful; annotate parameters and return types, let inference handle the rest

💼 Interview Questions

🎤 Mock Interview

Practice a live interview for Why TypeScript & Setup