Modeling Patch APIs with TypeScript exactOptionalPropertyTypes
Distinguish an omitted property from an explicit undefined value when object presence changes application behavior.
With exactOptionalPropertyTypes enabled, an optional property means the key may be absent; it does not automatically mean the present key may hold undefined. This distinction matches JavaScript operations that inspect key presence and is useful for partial update contracts.
THE PRODUCTION PROBLEM STATEMENT
In a patch request, omitting a field can mean “leave it unchanged,” while sending null can mean “clear it.” Treating absent, undefined, and null as interchangeable can accidentally erase values or produce ambiguous updates.
Give absence, undefined, and null deliberate meanings
Without this flag, an optional property often accepts an explicit undefined assignment even when the written type excludes it. At runtime, however, an absent key and a present key whose value is undefined behave differently with the in operator, Object.keys, and object spread.
Enable exactOptionalPropertyTypes together with strictNullChecks when that distinction matters. Add undefined to the declared union only if explicit undefined is genuinely part of the contract.
interface ProfilePatch {
displayName?: string | null;
}
function apply(patch: ProfilePatch) {
if ('displayName' in patch) {
if (patch.displayName === null) clearDisplayName();
else setDisplayName(patch.displayName);
}
}Design partial updates around key presence
A clear patch contract can use absence for “unchanged,” a value for “replace,” and null for “clear.” That is easier to validate and document than assigning several meanings to undefined.
When normalizing inputs, check whether a key exists before copying it. A truthiness check would incorrectly drop legitimate empty strings, false booleans, or zero values.
Adopt the option with a controlled migration
The option is not included automatically in TypeScript’s strict family, so enable it explicitly. Existing code may rely on assigning undefined to optional properties; fix those assignments or update types where undefined is intentionally supported.
Check library declarations and generated DTOs when upgrading. The flag catches mismatches in the static model, but untrusted JSON still needs runtime validation at the boundary.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
Not with exactOptionalPropertyTypes enabled. The optional marker permits omission; explicitly add undefined if the present property should accept it.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Type-Safe Primitives with Branded Types: Enforcing Domain Rules at Compile Time
Preventing accidental mixing of UserId, OrderId, and validated strings using zero-runtime-cost nominal branding in TypeScript.
Runtime Type Validation in TypeScript: Zod vs TypeBox vs Valibot for High-Throughput APIs
Evaluating schema validation performance, bundle size trade-offs, and static type inference across modern validation libraries.