lecture 6 of 110 completed

Typing API responses

Write a response type from real responses, not from memory. The type is still a claim about what the server sends, so keep that claim in one place.

What you'll learn

  • How to tell an optional field from a field that can be null
  • How to check a type against recorded sample responses
  • Where the type of a response is a claim, and how to keep it in one place

You write a type for the loan that the Shelf API returns. You write it from what you remember of the page.

interface Loan {
  id: number;
  book: string;
  returnedOn?: string;
  member: { name: string; email: string };
}

It compiles, and it would compile whatever you wrote. Nothing has compared it with the API yet. Here are two real responses.

{ "id": 7, "book": "Dune", "returnedOn": null, "member": { "name": "Ana" } }
{ "id": 8, "book": "Emma", "returnedOn": "2026-03-01", "member": { "name": "Ben", "email": "ben@example.test" } }

The type is wrong in two places, and both matter to a test.

Optional or null

Look at returnedOn in the first response. The field is there, and its value is null. Look at email. In the first response the field is not there at all.

Those are different, and TypeScript has a different type for each.

  • A field that may be absent is optional: email?: string.
  • A field that is always present and may hold null is a union: returnedOn: string | null.

A test that checks loan.returnedOn === undefined for an open loan passes or fails depending on which one the server really does. Read a response before you write the type.

Let the compiler compare

You can make the compiler check your type against the responses you recorded. Paste each one into a file as a typed constant.

interface Loan {
  id: number;
  book: string;
  returnedOn?: string;
  member: { name: string; email: string };
}

export const openLoan: Loan = { id: 7, book: "Dune", returnedOn: null, member: { name: "Ana" } };
loan-samples.ts(8,54): error TS2322: Type 'null' is not assignable to type 'string | undefined'.
loan-samples.ts(8,72): error TS2741: Property 'email' is missing in type '{ name: string; }' but required in type '{ name: string; email: string; }'.

Both mistakes are reported. Here is the type the samples agree with.

export interface Loan {
  id: number;
  book: string;
  returnedOn: string | null;
  member: { name: string; email?: string };
}

export const openLoan: Loan = { id: 7, book: "Dune", returnedOn: null, member: { name: "Ana" } };
export const returnedLoan: Loan = {
  id: 8,
  book: "Emma",
  returnedOn: "2026-03-01",
  member: { name: "Ben", email: "ben@example.test" },
};

Be clear about what this proves. The type now fits two responses that you copied on one day. It does not prove what the server sends tomorrow. It is still much better than a type written from memory, and the samples are useful as test data too.

Where the claim is made

In a test, the response arrives through the framework. This example is checked against Playwright's own types.

import type { APIRequestContext } from "@playwright/test";
import type { Loan } from "./loan";

export async function getLoan(request: APIRequestContext, id: number): Promise<Loan> {
  const response = await request.get("/api/loans/" + id);
  return response.json();
}

It compiles, and no line says as. response.json() returns any, and any fits a Loan with no error. The return type Promise<Loan> is the claim. Nothing in this function looks at the body.

You cannot avoid making that claim somewhere. You can decide where. Put it in one function like this one, in one file. Then every test gets a Loan from getLoan, and when the claim turns out wrong there is one place to fix. The next lecture replaces the claim with a check.

A response that is a loan or an error

Many APIs answer with a different body when something goes wrong.

{ "error": "Loan 99 does not exist" }

So the honest type of the body is a union. You can tell the two apart with in, from lecture 1.

interface Loan {
  id: number;
  book: string;
}

interface ApiError {
  error: string;
}

function describe(body: Loan | ApiError): string {
  if ("error" in body) {
    return "failed: " + body.error;
  }
  return "loan of " + body.book;
}

console.log(describe({ id: 7, book: "Dune" }));
console.log(describe({ error: "Loan 99 does not exist" }));
loan of Dune
failed: Loan 99 does not exist

This works, and it is fine for two shapes. There is a cleaner way to write a type that is one of several shapes, called a discriminated union. The Senior track teaches it. Until then, use in, and do not build larger designs on it.

Check the status code first in any case. A body is only worth reading as a Loan when the response says the request succeeded.

Remember this

Write a response type from real responses. An absent field is optional, and a field that holds null is a union with null. Typed sample constants let the compiler compare the type with recorded data. The type of a parsed body is still a claim, so make it in one function.

Reference: API testing in the Playwright documentation.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.