Direct answer
Apply assertNever in the default branch of a conditional or switch over a discriminated pagination response union, such as PageSuccess | PageEmpty | PageError, when adding a new response variant must fail compilation until every handler is updated. Use one type guard per discriminant value, checking only the discriminant field (for example status or kind). Check required fields such as items, total, page, and pageSize only after narrowing, and only when the server contract guarantees them. Keep guard logic in one shared module or contract-generated package and import it on both client and server; do not duplicate predicates.
When assertNever is appropriate
assertNever is useful only for statically known unions with a discriminant. It catches missing cases at compile time if a new union member is added and the default branch receives the new type. It does not validate network payloads, and it does not apply to optional or unstructured metadata fields.
Confirmed behavior
- TypeScript narrows a union when a type-guard function returns a type predicate.
- assertNever accepts a parameter of type never; any unhandled union branch left in the default case produces a type error.
- Type guards are erased at runtime and cannot confirm that remote JSON matches the narrowed shape.
Likely explanation
Pagination metadata often arrives as separate contracts for total counts, page indices, and item arrays. If each contract is typed independently, TypeScript cannot narrow across them reliably. A discriminated union forces every response variant to declare its shape through a single literal field, which makes exhaustiveness checking possible.
Minimal guard predicates
Define the smallest useful set:
- One guard per response variant that checks the discriminant field only.
- Optional presence checks for fields your code reads after narrowing, such as items or total, guarded by the server contract, not by runtime assumptions.
Do not create a separate guard for every metadata field. Over-narrowing optional fields makes predicates brittle: valid responses fail when the server omits or adds unrelated fields, and TypeScript still cannot prove runtime safety.
type PageSuccess = { kind: "success"; items: unknown[]; total: number; page: number; pageSize: number; };
type PageEmpty = { kind: "empty"; total: number; page: number; pageSize: number; };
type PageError = { kind: "error"; message: string; };
type PageResponse = PageSuccess | PageEmpty | PageError;
function isSuccess(response: PageResponse): response is PageSuccess {
return response.kind === "success";
}
function handle(response: PageResponse): void {
if (isSuccess(response)) {
// response narrowed to PageSuccess
return;
}
// handle other variants or exhaustiveness
assertNever(response);
}
function assertNever(value: never): never {
throw new Error(`Unhandled pagination response: ${JSON.stringify(value)}`);
}
Synchronizing guards across modules
- Publish response types and guards from one shared package or contract-generated module.
- Import the same guard functions in client and server code instead of copying predicate bodies.
- Add a CI test that imports the shared guard on both sides, renames a field, and confirms both sides fail together.
- Prefer runtime schema validation at the network boundary if the server schema is not controlled by the same build.
Assumptions and uncertainty
This answer assumes TypeScript 5.x-style type predicates and discriminated unions. If your pagination responses do not share a stable discriminant field, assertNever will not apply and you should ask whether a discriminant can be added to the server contract. Runtime shape drift remains possible unless a decoder such as Zod or io-ts validates the payload at the boundary.
Verification
- Add a new member to the PageResponse union and confirm TypeScript reports an error in the assertNever default branch.
- Send a malformed paginated JSON payload through the guard and confirm narrowed field access is rejected by a runtime validator or remains typed as unknown.
- Import the same guard and schema from a shared module in client and server tests, rename a field, and confirm both sides fail together instead of drifting silently.
Missing diagnostic detail: Do your pagination responses include a stable discriminant field such as kind or status? If not, exhaustiveness checking cannot be applied without changing the server contract.