Advanced TypeScript for Harness Builders
3. Exhaustive Checks with `never`
Compile-time completeness: the compiler errors at the default branch when a new union member is added.
Assign the switch default to a never-typed variable. If a new union member is added, the compiler raises an error here.
File: packages/ai/src/providers/google-shared.ts L307-310
default: {
const _exhaustive: never = reason;
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
}
File: packages/coding-agent/src/core/messages.ts L188-191
default:
const _exhaustiveCheck: never = m;
return undefined;
This pattern guarantees at compile time that every branch of a discriminated union is handled.