|

Day 45 of 100: Utility Types — Partial, Pick, Omit for Test Variants

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


Utility types transform existing types. Test data variants without redefining shapes.

Contents

The Big Four

type User = { id: number; name: string; email: string; role: string };

// Partial — everything optional → override objects
type UserOverrides = Partial;

// Pick — subset of fields
type UserPreview = Pick;

// Omit — everything except
type NewUser = Omit;          // creation payload — no id yet

// Required — strip all optionality
type CompleteUser = Required;

🚀 Level Up Your Playwright

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

The Factory Pattern These Enable

function makeUser(overrides: Partial = {}): User {
  return {
    id: Date.now(),
    name: 'Default Tester',
    email: `qa-${Date.now()}@test.com`,
    role: 'viewer',
    ...overrides,                       // spread — Day 14, now typed
  };
}

const admin = makeUser({ role: 'admin' });
const noEmail = makeUser({ email: '' });
// makeUser({ rol: 'admin' })  — error: typo caught by Partial

Readonly and Record Preview

type FrozenConfig = Readonly<{ baseUrl: string }>;
const cfg: FrozenConfig = { baseUrl: 'http://localhost' };
// cfg.baseUrl = 'x'  — error: readonly

type EnvUrls = Record<'local' | 'staging' | 'prod', string>;  // Day 46

Tomorrow (Day 46): Record and Readonly — typed lookup tables.

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