Why TypeScript & Setup
📖 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:
- You write
.tsfiles with type annotations - The TypeScript compiler (
tsc) type-checks the code - Types are completely erased — the output is plain JavaScript
- 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
1// Install TypeScript2// npm install -g typescript3// npx tsc --init (creates tsconfig.json)45// Basic type annotations6let message: string = "Hello, TypeScript!";7let count: number = 42;8let isActive: boolean = true;910// Type inference — TS figures out the type automatically11let inferred = "TypeScript infers this as string"; // type: string12let num = 100; // type: number1314// Function with type annotations15function greet(name: string): string {16 return `Hello, ${name}!`;17}1819// greet(42); // ❌ Compile error: Argument of type 'number'20// // is not assignable to parameter of type 'string'21greet("Alice"); // ✅ Works2223// The compiled JavaScript output (types are erased):24// function greet(name) {25// return `Hello, ${name}!`;26// }2728// tsconfig.json essentials (starter):29// {30// "compilerOptions": {31// "target": "ES2022",32// "module": "ESNext",33// "strict": true, // Enable all strict checks34// "noUncheckedIndexedAccess": true, // Arrays may be undefined35// "esModuleInterop": true,36// "outDir": "./dist",37// "rootDir": "./src"38// },39// "include": ["src/**/*"]40// }
🏋️ Practice Exercise
Mini Exercise:
- Install TypeScript globally and run
tsc --initto create a tsconfig - Create a
.tsfile that declares variables of each primitive type - Write a function
calculateArea(width: number, height: number): number - Try passing wrong types — observe the compile-time errors
- Compile with
tscand 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
anyeverywhere to 'make it work' — this defeats the entire purpose of TypeScriptNot enabling
strict: truein tsconfig — without it, many type checks are disabled and you lose most of TS's valueConfusing 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