Why TypeScript in a test framework
TypeScript gives autocompletion, safe refactoring and early errors, but types exist only at development time and vanish at runtime. So they do not validate an API response: at the network boundary take data as unknown and narrow it with a check or a schema, not as. Playwright only transpiles TS without type checking, so CI needs a separate tsc --noEmit step, and tsconfig needs strict.
TypeScript · Basics
The Transfer type is defined, the test compiles, yet the sum is glued as a string. Why is TypeScript silent?
A test framework has a Transfer type with amount: number. A test takes an API response, adds the amount and the fee and compares the result with the expected value. Everything compiles, the editor underlines nothing, and the test fails: the server returns the amount as the string "100.50", so instead of addition there is concatenation. The type "lied" and nobody noticed. This lesson is about what TypeScript guarantees, what it does not, and how to build a test framework so that types really help.
How it works and why. TypeScript is JavaScript with a type system that works only at development time. Types, interfaces and annotations do not reach the executable code: after transpilation plain JS remains. This is a deliberate design: TS is added to existing JS without changing the runtime, so it knows nothing about what actually arrives from the network. A second consequence matters more for QA: Playwright transpiles TS but does not check types (its documentation says so). A test with a type error can run happily. Types are checked by a separate step, tsc --noEmit, and its place is in CI and in the editor.
Where types live
What this gives an automation engineer once the tsc step is set up. Autocompletion for Page Objects, fixtures and API clients: a new engineer sees which methods exist and what they accept. Safe refactoring: rename the accountId field and the compiler shows every broken test. The TransferRequest type documents the contract better than a comment. Early errors: a typo in a field name, undefined where a string is expected, a promise result used as a value without await. A bare call without await passes the compiler; the ESLint rule @typescript-eslint/no-floating-promises catches it.
Basic primitives: string, number, boolean, null, undefined, bigint, symbol. Arrays are written string[] or Array<string>, tuples [string, number]. You do not annotate every variable: TS infers the type. Annotations go on boundaries: function parameters, the values public methods return, data models. "strict": true in tsconfig turns on, among others, noImplicitAny and strictNullChecks. Without it half of the value is lost, so a new project has it on from day one.
{
"compilerOptions": {
"strict": true,
"noEmit": true
}
}
Example: the code compiles and a bug hides. A typical test checks a transfer sum with a fee:
interface Transfer {
id: string;
amount: number;
fee: number;
}
test('total with fee', async ({ request }) => {
const res = await request.post('/api/transfers', { data: { amount: 100.5, fee: 0.05 } });
const body = (await res.json()) as Transfer;
const total = body.amount + body.fee;
expect(total).toBe(100.55);
});
What is wrong here. The as Transfer line checks nothing: it is a promise to the compiler "trust me, the shape is this". res.json() in Playwright does not know the response shape (in the types it is a value with no checked structure, effectively any), so as always passes. If the server returns amount as a string, body.amount + body.fee gives the string "100.500.05", not a number. The test fails comparing a string with a number, and the cause has to be found by hand: the error points at the test, not at the mismatch between the type and the contract.
The fix is to move the check to where data enters the framework: accept the response as unknown and narrow the type with an explicit check.
function isTransfer(v: unknown): v is Transfer {
if (typeof v !== 'object' || v === null) return false;
const t = v as Record<string, unknown>;
return (
typeof t.id === 'string' &&
typeof t.amount === 'number' &&
typeof t.fee === 'number'
);
}
const body: unknown = await res.json();
if (!isTransfer(body)) {
throw new Error('Not a Transfer: ' + JSON.stringify(body));
}
const total = body.amount + body.fee;
Now the mismatch is caught on the very first response, and the message contains the raw data. After that body has the type Transfer without any as, because the compiler sees that the check passed. For large models the function is not written by hand: a schema (zod, ajv) gives both the runtime check and the type from one description. The principle is the same: a type describes an expectation, and a runtime check confirms that the data matches it.
Trusting API data
as Transfer
The type is declared, not verified. The error shows up later in the test and looks like a logic failure.
unknown and a check
The mismatch is visible at once on input, the message carries the raw data, and afterwards the type is real.
Limits and alternatives. Types do not replace tests and do not check the runtime: they prove only that the code is consistent with itself. A wrong type written by hand will not be exposed by the compiler. So the value is not in the number of annotations but in their accuracy and in the boundaries where they are confirmed by data. Typing has a cost: more code, a slower start for a new engineer, the need to keep types up to date. In small scripts and one-off checks, JS without types is a reasonable choice. For a framework that lives for years and is changed by many people, types pay off during refactors. A gradual path exists: allowJs and migration file by file.
Common mistakes.
- Assuming Playwright catches type errors itself: it only transpiles, so without
tsc --noEmitin CI type errors reach the test run. as Typeon an API response: there is no check, and the promise to the compiler becomes a source of false confidence.strictturned off:undefinedandnullstop being part of the type and half the errors slip through.- Annotating everything: noise with no benefit, because TS infers the type anyway.
- Types without upkeep: the model lags behind the API and "safe" code is safe on paper only.
Check yourself
A test with const body = (await res.json()) as Transfer is green locally, but CI shows the server returns amount as a string. Why did neither TypeScript nor the editor say so?
Answer
Because as is an assertion to the compiler, not a check. Types are erased after transpilation, and at runtime no code compares the response with the interface. The fix: accept the response as unknown and verify the shape (a guard or a schema) on input.
A test has an obvious type error, yet npx playwright test runs it. Is Playwright set up wrongly, and what should be done?
Answer
There is no mistake: Playwright transpiles TS without checking types. Add type checking as a separate tsc --noEmit step in CI (and in the editor), so errors are caught before the tests run.
unknown and narrow it with a check or a schema instead of writing as. I also remember that Playwright does not check types: CI has a separate tsc --noEmit step and strict is on in tsconfig.