|

Day 63 of 100: Strict-Migration Exercise — TS Phase Finale

Day 63 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


Final TS day: a migration exercise. One loose file, strict mode on, every error fixed with phase patterns.

Contents

The Loose File (Before)

// loose.ts — compiles only without strict
function process(data) {                       // implicit any
  return data.items.map(i => i.name);
}

let config = { url: 'http://localhost' };
config = { url: 'http://x', extra: true };     // shape drift

async function load(id) {
  const res = await fetch('/api/items/' + id);
  return res.json();                            // any escapes
}

const first = [].find(x => x);                  // never[] weirdness
first.toString();                               // possible undefined

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

The Strict Fix (After)

type Item = { name: string };
type ItemList = { items: Item[] };

function isItemList(v: unknown): v is ItemList {
  return typeof v === 'object' && v !== null &&
    Array.isArray((v as any).items) &&
    (v as any).items.every((i: unknown) =>
      typeof i === 'object' && i !== null && typeof (i as any).name === 'string');
}

function process(data: ItemList): string[] {
  return data.items.map(i => i.name);
}

type Config = { url: string };
const config: Config = { url: 'http://localhost' };   // const + typed

async function load(id: number): Promise {
  const res = await fetch(`/api/items/${id}`);
  if (!res.ok) throw new Error(`${res.status} loading item ${id}`);
  const body: unknown = await res.json();
  if (!isItemList(body)) throw new Error('bad shape');
  return body;
}

const nums = [3, 1, 2];
const first = nums.find(n => n > 1);
if (first !== undefined) first.toFixed(0);            // narrowed

TypeScript phase complete — 30 days. Tomorrow the main event: Playwright, on a foundation that can carry it.

Tomorrow (Day 64): Playwright begins — install, first test, architecture.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.