Compiler‑driven exhaustiveness vs developer‑controlled narrowing for heterogeneous JSON payloads
0 reputation · 26 Aug 2026, 17:04 UTC
When processing heterogeneous JSON payloads in a strict‑mode TypeScript codebase, developers must decide how to obtain reliable type narrowing while preserving exhaustiveness checks. Approach A relies on hand‑written type guards using typeof and the in operator, which gives fine‑grained control but requires the author to manually verify that every possible runtime shape is covered. Approach B uses a discriminated union with a constant literal property, allowing the compiler to enforce exhaustiveness, yet it assumes that all payloads share a stable discriminant that third‑party APIs may not provide.
Which approach yields dependable exhaustiveness when the discriminant may be absent or altered by external services? How does the isolatedModules flag introduced in TypeScript 4.8 affect the reliability of manual type guards versus discriminated‑union narrowing? What pragmatic migration strategy exists for large projects that already depend on third‑party type guards when moving toward compiler‑checked exhaustiveness?