089
LVL 04 — SENIOR-IN-TRAININGSESSION 089DAY 89

TYPESCRIPT INTRO

🎫 PIXELCRAFT-075
🐛Fix | 🟠 Hard | Priority: 🟠 High

Three bugs shipped to production last month — all caused by passing wrong types: a string where a number was expected, an undefined property access, a missing function argument. TypeScript catches these at compile time. Convert the 5 most critical files. TS finds 3 existing bugs during migration.
CONCEPTS.UNLOCKED
🔷
TypeScript
JavaScript + types. A superset of JS that adds type annotations. Compiles to plain JavaScript. Catches bugs before you run the code. Used by every major tech company.
📋
Interfaces
Define the shape of objects. interface User { id: string; name: string; email: string; } — now every function that takes a User knows exactly what properties exist.
🏷️
Type Annotations
Tell TypeScript what you expect. function add(a: number, b: number): number — the compiler rejects add("hello", 42) before it ever runs. Bugs caught at write time, not runtime.
🧬
Generics
Types that take parameters. Array<string>, Promise<User>, Map<string, Image>. Write one function that works with any type — while still being type-safe.
🔀
Union Types
A value that can be one of several types. type Status = 'loading' | 'success' | 'error'. TypeScript forces you to handle every case — no forgotten states.
🔄
Gradual Migration
Convert file by file, not all at once. Rename .js → .ts. Fix the errors. Repeat. TypeScript allows JS and TS to coexist — migrate at your own pace.
HANDS-ON.TASKS
01
Set Up TypeScript
npm install -D typescript @types/node @types/react @types/express npx tsc --init // tsconfig.json (key settings) { "compilerOptions": { "target": "ES2022", "module": "ESNext", "strict": false, // ↑ Start lenient, tighten later "allowJs": true, // ↑ JS and TS coexist "jsx": "react-jsx", "outDir": "./dist", "rootDir": "./src", "esModuleInterop": true, "resolveJsonModule": true, "skipLibCheck": true, }, "include": ["src/**/*"], }
allowJs: true is the key to gradual migration. TypeScript will compile .js files too — you don't need to convert everything at once.
02
Define Core Interfaces
// types/models.ts export interface User { id: string; name: string; email: string; createdAt: Date; role: 'user' | 'admin'; } export interface Image { id: string; title: string; owner: string; // User.id originalPath: string; thumbnailPath?: string; // ↑ ? means optional width: number; height: number; fileSize: number; format: 'jpeg' | 'png' | 'webp' | 'gif'; status: 'processing' | 'ready' | 'failed'; createdAt: Date; } export interface Filter { name: string; type: 'convolution' | 'colorMatrix' | 'custom'; kernel?: number[]; matrix?: number[]; params?: Record<string, number>; } export interface EditorState { currentImage: Image | null; activeFilter: Filter | null; history: ImageData[]; historyIndex: number; zoom: number; isDirty: boolean; }
03
Convert Filter Functions
// filters.ts (was filters.js) export function applyConvolution( imageData: ImageData, kernel: number[] ): ImageData { const { data, width, height } = imageData; const output = new Uint8ClampedArray(data); const kSize = Math.sqrt(kernel.length); // TS catches: kernel.length must // be a perfect square (3×3 or 5×5) if (kSize % 1 !== 0) { throw new Error( 'Kernel must be square'); } // ... rest of convolution return new ImageData( output, width, height); } export function applyColorMatrix( imageData: ImageData, matrix: number[] ): ImageData { if (matrix.length !== 9) { // TS + runtime check throw new Error( 'Color matrix must have 9 values'); } // ... return imageData; }
04
TypeScript Catches 3 Existing Bugs
// BUG 1: Wrong argument type // ❌ Was: passing string to number param applyVignette(imageData, "0.5"); // TS error: Argument of type 'string' // is not assignable to type 'number' // ✅ Fix: applyVignette(imageData, 0.5); // BUG 2: Accessing undefined property // ❌ Was: image.thumbnail (wrong name) const url = image.thumbnail; // TS error: Property 'thumbnail' does // not exist on type 'Image'. // Did you mean 'thumbnailPath'? // ✅ Fix: const url = image.thumbnailPath; // BUG 3: Missing function argument // ❌ Was: called with 1 arg, needs 2 applyConvolution(imageData); // TS error: Expected 2 arguments, // but got 1. // ✅ Fix: applyConvolution(imageData, kernel);
These three bugs existed in the JS codebase for weeks. They'd crash at runtime for specific users. TypeScript caught all three the moment the files were converted — before any code ran.
05
Close the Ticket
git switch -c feature/PIXELCRAFT-075-typescript-intro git add src/ types/ tsconfig.json git commit -m "Convert 5 critical files to TypeScript, fix 3 bugs (PIXELCRAFT-075)" git push origin feature/PIXELCRAFT-075-typescript-intro # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Type systems are the most effective bug prevention tool in software engineering.

Studies show that ~15% of JavaScript bugs could be caught by TypeScript at compile time — before a single line of code runs.

// Type systems spectrum:

Untyped
  Assembly, early JS
  No protection. Bugs at runtime.

Dynamically typed
  JavaScript, Python, Ruby
  Types exist at runtime only
  "Duck typing" — flexible but risky

Gradually typed
  TypeScript, Python + mypy
  Opt-in types. Best of both worlds.

Statically typed
  Java, C#, Go, Rust
  Compile-time guarantees
  More verbose, more safe

// TypeScript = JavaScript's safety net
// Same language, new superpowers.
"TypeScript Lab"
[A]Convert the undo system to TypeScript. Define a generic UndoStack<T> that works with any state type. The type system should prevent pushing wrong state shapes onto the stack.
[B]Add TypeScript to your Zod schemas (from session 082). Use z.infer<typeof schema> to auto-generate types. Compare: manually written interfaces vs Zod-inferred types. Which is the source of truth?
[C]Research: TypeScript's structural typing vs Java's nominal typing. In TS, two interfaces with the same shape are compatible — even if they have different names. Why? What are the tradeoffs?
REF.MATERIAL
ARTICLE
Microsoft
The official TypeScript guide: types, interfaces, generics, narrowing, modules, and configuration. Start here for any TS question.
TYPESCRIPTOFFICIALESSENTIAL
VIDEO
Fireship
Ultra-fast TypeScript overview: what it is, why it exists, and how it prevents bugs. The quickest possible introduction.
TYPESCRIPTQUICK
ARTICLE
Matt Pocock
Advanced TypeScript patterns, tips, and exercises. The best resource for leveling up beyond the basics: generics, type narrowing, template literals.
TYPESCRIPTADVANCEDESSENTIAL
ARTICLE
Anthony Fu
Practice TypeScript's type system with interactive challenges. From easy (Pick, Readonly) to extreme (parser combinators in types). Playground-style learning.
PRACTICEINTERACTIVE
VIDEO
freeCodeCamp
Comprehensive TypeScript course: setup, types, interfaces, generics, enums, decorators, and real-world project integration.
TYPESCRIPTTUTORIAL
// LEAVE EXCITED BECAUSE
Five files converted. Three existing bugs caught instantly — before the code even ran. Your editor now autocompletes every property, warns about every type mismatch. TypeScript is your new pair programmer.