You are reviewing a TypeScript client that fetches order data. It compiles in strict mode but crashes when a field is missing. To diagnose it, distinguish relationships the compiler can verify from claims the program still has to check at runtime.
Make the trust boundary visible
1. Why start with unknown rather than any?
An unknown value can represent data you have not checked, but you cannot safely access its properties without narrowing. any bypasses that protection and can spread through dependent expressions. An assertion like payload as Order does not validate JSON or convert it to an order. Check the fields you need at the boundary, including that an object is non-null and that its fields have the expected types.
2. Does calling a generic fetch helper validate the response?
No. A helper returning Promise<T> cannot inspect T at runtime. A decoder parameter lets the caller supply the missing runtime work. In this example, the caller's decoder determines the inferred return type, and the helper checks HTTP status independently of decoding.
async function load<T>(
url: string,
decode: (payload: unknown) => T,
): Promise<T> {
const response = await fetch(url);
if (!response.ok) throw new Error('Request failed');
const payload: unknown = await response.json();
return decode(payload);
}
The function still rejects on invalid JSON or a decoder error. Decide at the call site which failures to show as transport issues and which to surface as a broken contract. A decoder that only casts the value defeats the design.
Represent relationships explicitly
3. When does a constraint add value?
Consider a helper that reads a selected property: function get<T, K extends keyof T>(item: T, key: K): T[K]. The constraint ties permitted keys to the specific input and keeps the selected property's type in the return value. A helper accepting any string cannot make that same promise without a runtime check. A generic parameter that appears only in the return position can instead let the caller claim a type the function has no evidence for.
4. How can one field make a union easier to narrow?
Use a discriminant with literal values. Model success as { kind: 'ready'; order: Order } and failure as { kind: 'failed'; message: string }. Checking kind narrows the available data. This prevents a type that permits “ready” and “missing order” at the same time. An exhaustive switch with a never check can make a newly added variant a compile-time error at its consumers.
5. What can a wrong type predicate hide?
A hand-written predicate may claim that an object is an order after checking only that an ID exists. Callers then trust even the fields that were not checked. Test decoders with missing, null, and wrong-type fields. Also avoid using a truthiness check where zero or an empty string is valid; test for the exact condition you mean.
Practice from the outside in
- Feed the decoder a valid order, a malformed object, and an array. State which branch rejects each invalid value.
- Replace a generic return-only assertion with a decoder parameter; name what is inferred and what is validated.
- Add a third response variant and find the consumers that must change.
For the runtime side of the exercise, read the JavaScript scheduling walkthrough. Then use Mentaro's free practice demo to rehearse explaining the boundary between types and runtime behavior.