Sub topic 14
When Narrowing Is Lost: Callbacks, Mutation, and Aliasing
What you need to know
This chapter assumes you understand TypeScript functions, union types, if statements, callbacks, and basic control flow analysis. Narrowing means that TypeScript uses a check to replace a wide type with a more specific type. For example, a string | undefined value becomes string after a check that excludes undefined.
Narrowing is based on facts that the compiler can trust. If another operation might change a value, TypeScript removes a narrowing rather than risk an invalid property access.
Callbacks can run later
A callback can run after the surrounding code finishes. Between the check and the callback, another part of the program might change a mutable value.
function logName(user: { name?: string }): void {
if (user.name !== undefined) {
setTimeout(() => {
console.log(user.name.toUpperCase());
}, 0);
}
}Inside the if block, user.name is known to be a string. Inside the callback, TypeScript may report that user.name could be undefined. The property might have changed before the callback runs.
Copy the narrowed value into a local const instead:
function scheduleLog(user: { name?: string }): void {
const name = user.name;
if (name !== undefined) {
setTimeout(() => {
console.log(name.toUpperCase());
}, 0);
}
}The callback captures name, and name cannot be reassigned. The narrowing therefore remains useful inside the callback.
Mutation invalidates assumptions
A narrowing only describes the value at a particular point in control flow.
type Box = { value: string | undefined };
function clearAndUse(box: Box): void {
if (box.value !== undefined) {
box.value = undefined;
box.value.toUpperCase();
}
}After the assignment, box.value is again possibly undefined, so calling toUpperCase is unsafe. Check the property after the mutation, or save the original value before changing the object.
function useBeforeClearing(box: Box): void {
const value = box.value;
if (value !== undefined) {
console.log(value.toUpperCase());
box.value = undefined;
}
}The local value keeps the original string while box.value changes.
Aliases can hide changes
Two variables can refer to the same object:
function printName(user: { name?: string }): void {
const alias = user;
if (user.name !== undefined) {
alias.name = undefined;
user.name.toUpperCase();
}
}The assignment through alias changes the same object as user. TypeScript must account for this possibility, so property narrowing can disappear after mutation or across a callback.
A useful rule is to narrow a local value rather than repeatedly reading a mutable property:
A practical restructuring pattern
When narrowing disappears:
- Copy the value into a
constimmediately after checking it. - Avoid mutating the object until you finish using the narrowed value.
- Perform a fresh check after a mutation.
- Pass the narrowed value as a callback argument when possible.
- Use immutable data when a value must remain stable across asynchronous work.
function runLater(value: string | number): void {
if (typeof value === "string") {
const text = value;
setTimeout(() => {
console.log(text.toUpperCase());
}, 0);
}
}The local text records the fact that the original value was a string. The callback does not need to repeat the narrowing.
Exercise
Why can user.name lose its string narrowing inside a callback, and what simple change usually preserves it?
Show answer
The callback may run after user.name changes. Copy the property to a const, check that local value, and use the local value inside the callback.