[TypeScript]PUBLISHED 2026-11-25//4 min read//VERIFIED 2026-11-25

Modeling Patch APIs with TypeScript exactOptionalPropertyTypes

Distinguish an omitted property from an explicit undefined value when object presence changes application behavior.

EXECUTIVE ARCHITECTURAL OVERVIEW

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.

SECTION 01 //

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.

patch.ts
interface ProfilePatch {
  displayName?: string | null;
}

function apply(patch: ProfilePatch) {
  if ('displayName' in patch) {
    if (patch.displayName === null) clearDisplayName();
    else setDisplayName(patch.displayName);
  }
}
This shape allows omission or a string, while null explicitly means clear the value.
SECTION 02 //

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.

SECTION 03 //

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.

TECHNICAL INQUIRY MATRIX

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.

Official References & Specifications:
Faisal Askari

Faisal Askari

Verified Author

Software Engineer & Full-Stack Developer

Personal Website

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.

SPEC // VERIFIED ENTITY•Karachi, Pakistan (Global Remote)
contact@faisalaskari.top
05/RELATED ENGINEERING GUIDES
CONTINUE STUDY