|

Day 48 of 100: unknown vs any — The Safety Line

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


any disables TypeScript. unknown keeps it honest. One habit separates typed suites from JS-with-extra-steps.

Contents

The Difference

let a: any = JSON.parse('{}');
a.totally.fake.path;        // compiles! crashes at runtime

let u: unknown = JSON.parse('{}');
// u.anything               — error: must narrow first
if (typeof u === 'object' && u !== null && 'id' in u) {
  // narrowed — now usable
}

🚀 Level Up Your Playwright

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

Where any Sneaks In

  • res.json() resolves any in many setups — annotate or guard immediately
  • Untyped third-party libs — add @types/* or local declarations
  • Quick casts (as any) during debugging — they outlive the debugging
  • Catch variables — use catch (e: unknown) and narrow

Containment Strategy

// tsconfig: "strict": true, plus noImplicitAny
// ESLint: @typescript-eslint/no-explicit-any

// Boundary discipline:
async function getJson(url: string, guard: (v: unknown) => v is T): Promise {
  const res = await fetch(url);
  const body: unknown = await res.json();
  if (!guard(body)) throw new Error(`Unexpected shape from ${url}`);
  return body;                       // typed and VERIFIED
}

Tomorrow (Day 49): Type assertions — as, and when casting lies.

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