An absent property and a present property holding undefined can behave differently. Compiler settings such as exactOptionalPropertyTypes help express that distinction.
Before you start
You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.
The practical goal is to reason through this situation: A patch omitting name means leave it unchanged; an explicit null may mean clear it. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Define patch semantics
Distinguish omitted fields, replacement values and explicit clearing. These meanings must be consistent between client serialization and server updates.
Step 2: Represent the distinctions
An optional property models omission; a null union can model clearing. Enable exactOptionalPropertyTypes if explicit undefined should not count as an allowed value.
Step 3: Apply only present updates
Check property presence before changing persisted data. A truthiness check would incorrectly skip an intentionally empty replacement string.
Worked scenario
A patch omitting name means leave it unchanged; an explicit null may mean clear it.
type Patch = { name?: string | null };
function applyName(current: string | null, patch: Patch) {
return 'name' in patch ? patch.name : current;
}
console.log(applyName('Ada', {})); // Ada
console.log(applyName('Ada', { name: null })); // null
console.log(applyName('Ada', { name: '' })); // empty stringThe examples distinguish three operations rather than collapsing them into a single fallback. Validate incoming patches before invoking this helper; compiler settings do not constrain arbitrary JSON.
Common mistake
JSON serialization drops undefined-valued object properties.
Verify the behavior
Serialize an object containing undefined and observe that its property disappears. Confirm omission leaves the existing name intact while null clears it.
Interview exercise
Design a patch interface.
Answer and reasoning
Specify absence, clearing and replacement separately, and mirror those rules in runtime validation and persistence.
Continue learning
Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.