|

Day 36 of 100: Type Aliases — Typing Your Test Data

Day 36 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.


Type aliases name object shapes. Your test data gets a contract.

Contents

Type Aliases

type User = {
  id: number;
  name: string;
  email: string;
  role: 'admin' | 'viewer';   // union of literals — only these two!
  tags?: string[];             // optional property
};

const tester: User = {
  id: 1,
  name: 'Jane',
  email: 'jane@test.com',
  role: 'admin',
};

// tester.role = 'superuser'  — error: not in the union
// tester.emial = 'x'          — error: typo caught!

🚀 Level Up Your Playwright

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

Nested and Composed

type Address = { city: string; zip: string };

type Customer = {
  user: User;
  address: Address;
  orders: number[];
};

function summarize(c: Customer): string {
  return `${c.user.name} (${c.address.city}) — ${c.orders.length} orders`;
}

Why This Transforms Test Data

  • Factory returns User → every consumer knows the exact shape
  • API change (rename field) → compiler lists every affected test
  • role: ‘admin’ | ‘viewer’ → invalid roles impossible to construct

Tomorrow (Day 37): Interfaces — and the interface vs type debate settled.

🎓 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.