Day 51 of 100: Declaration Files — Typing the Untyped
Day 51 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.
Declaration files (.d.ts) describe types for JavaScript code. They explain how Playwright gives you autocomplete — and how to type untyped libraries.
Contents
What .d.ts Files Are
// math-utils.js (plain JS, no types)
// export function round2(n) { return Math.round(n * 100) / 100; }
// math-utils.d.ts — the type companion:
export declare function round2(n: number): number;
// Now TS users of math-utils.js get full checking.
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Where Types Come From
- Bundled: @playwright/test ships its own .d.ts — autocomplete out of the box
- DefinitelyTyped: npm install -D @types/node for untyped packages
- Your own: declare modules for internal JS utilities during migration
Ambient Declarations for Test Globals
// global.d.ts — typing env vars your tests rely on
declare namespace NodeJS {
interface ProcessEnv {
BASE_URL: string;
TEST_USER_EMAIL: string;
TEST_USER_PASSWORD: string;
CI?: string;
}
}
// process.env.BASE_URL is now string, typo'd names error out.
Tomorrow (Day 52): Exhaustive checking with never — unions that cannot drift.
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
