TypeScript / Editorial solution guide

A typed API response fails at runtime

Diagnose a mismatch between declared response types and actual JSON; validate the boundary and verify rejected cases.

About 3 min readContent reviewed

Read-only knowledge base · No public comments

Recognize the symptoms

A response compiles as a declared interface, but a property is missing, has the wrong type, or causes a later calculation to fail. The failure may appear only for one producer or a new response version. Preserve a redacted synthetic fixture and the first incorrect boundary value.

Understand the likely causes

  • A type annotation or assertion does not inspect JSON at runtime. An upstream producer can omit a field, send null, or change its representation while checked application code continues to assume the declared shape.

Locate the first untrusted boundary

Reproduce with a minimal response and identify where parsing becomes a trusted domain value. Inspect the actual field types and presence without logging private payloads. Compare the producer contract, response version, and consumer declaration. If a field is absent, determine whether it is legitimately optional or whether the producer violated the contract; avoid adding a default until that decision is explicit.

Replace the assertion with validation

Treat the parsed value as unknown and check object shape, required properties, and domain constraints before creating a trusted object. The small quantity validator illustrates a tagged success or failure result; a real response validator must also check the surrounding object and all relevant fields. Keep the rejection observable to the caller, and decide whether a numeric string is invalid or needs a separately documented conversion.

typescript Validate an unknown quantity
type Result = { ok: true; value: number } | { ok: false; error: string };
function readQuantity(input: unknown): Result {
  if (typeof input !== "number" || !Number.isSafeInteger(input) || input < 1) {
    return { ok: false, error: "invalid quantity" };
  }
  return { ok: true, value: input };
}

The success branch carries a positive safe integer; other values return a visible rejection.

Handle failures and preserve meaning

Return a field-specific safe error or propagate an integration failure according to the caller’s contract. Do not replace every malformed response with an empty successful result, because that hides a producer defect. If the API uses versions, validate the version first and map each supported variant to the internal model. Coordinate schema changes with the producer and define how unsupported versions are reported.

Verify both sides of the boundary

Run valid, missing, null, wrong-type, fractional, and out-of-range fixtures. Confirm invalid values cannot reach the calculation and the valid case still behaves as expected. Add a compile-time test for the returned union and a runtime test for malformed data; neither substitutes for the other. Finally exercise a representative response from a controlled integration environment and record the contract version and expected behavior.

Verify the fix

Verify the fixture matrix and assert that each failure follows the documented error path. Check that no sensitive response body enters logs and that the valid path still succeeds.

Keep it from recurring

  • Keep producer and consumer contracts versioned, validate at every trust boundary, and retain malformed fixtures as regression tests. Review assertions as claims requiring evidence, not as runtime checks.

Official references