|

Day 40 of 100: Typing Functions — Params, Returns, Async Signatures

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


Function types make helpers self-documenting. Async return types prevent missing-await bugs.

Contents

Signatures

function retry(action: () => boolean, max: number = 3): boolean {
  for (let i = 0; i < max; i++) if (action()) return true;
  return false;
}

// Async ALWAYS returns Promise
async function fetchUser(id: number): Promise<{ id: number; name: string }> {
  return { id, name: 'Jane' };
}

// Function type aliases
type Validator = (value: string) => boolean;
const nonEmpty: Validator = v => v.length > 0;

🚀 Level Up Your Playwright

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

void and never

function logResult(msg: string): void {     // returns nothing
  console.log(msg);
}

function fail(msg: string): never {          // never returns — always throws
  throw new Error(msg);
}

QA Shapes You Will Write

type TestFn = (page: unknown) => Promise;

async function withTiming(label: string, fn: () => Promise): Promise {
  const t0 = Date.now();
  const result = await fn();
  console.log(`${label}: ${Date.now() - t0}ms`);
  return result;
}
// const users = await withTiming('fetch', () => api.getUsers());

Tomorrow (Day 41): Generics — one helper, every type.

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