When TypeScript’s exactOptionalPropertyTypes option is disabled, an optional property already accepts undefined; adding
| undefined repeats the same contract. Remove the redundant union member while keeping ? if callers should still be able to
omit the property. This rule does not report issues when exactOptionalPropertyTypes is enabled, because the two declarations then have
different meanings.
interface Person {
address?: string | undefined; // Noncompliant: "undefined" is redundant
}
interface Person {
address?: string;
}
Choose the declaration that matches the intended contract:
address?: string: callers may omit the property. With exactOptionalPropertyTypes enabled, explicitly assigning
undefined is rejected.address: string | undefined: callers must supply the property, but may give it an undefined value. Removing
? makes the property required, which changes the contract and may require changes at call sites.address?: string | undefined: with exactOptionalPropertyTypes enabled, callers may omit the property or explicitly
assign undefined. Both parts are needed for that contract, so the rule does not report it.These distinctions assume strictNullChecks is enabled. Without it, undefined is assignable to other types as well.
Note that reading a missing property returns undefined; TypeScript does not add that property to the object.
The rule also follows local type aliases, alias chains, and generic types that introduce undefined. This requires
strictNullChecks or strict; otherwise, only undefined written directly in the property annotation is
detected.
For a local alias such as type Maybe<T> = T | undefined, decide whether to change the alias for all its users or use a narrower
type for the optional property. Do not remove ? just to avoid changing an alias if omission is part of the contract.
The rule does not report undefined reached only through an external library’s type, including indexed access to an external optional
property:
import type { TextFieldProps } from "@mui/material";
interface Props {
size?: TextFieldProps["size"]; // Not reported: the type comes from an external declaration
}
There is no explicit union member to remove here, and removing ? would make size required. This exception does not apply
to the same pattern using a locally declared type.