Skip to main content

require-exact-optional-property-types

Require "exactOptionalPropertyTypes": true in compilerOptions to enforce that optional properties cannot be explicitly set to undefined unless undefined is part of the property's declared type.

Targeted pattern scopeโ€‹

The compilerOptions.exactOptionalPropertyTypes field in any tsconfig*.json file.

What this rule reportsโ€‹

This rule reports when compilerOptions is present and exactOptionalPropertyTypes is absent or set to false. The rule does not report if compilerOptions is not defined at all.

Why this rule existsโ€‹

Without exactOptionalPropertyTypes, TypeScript treats { prop?: string } as equivalent to { prop?: string | undefined }. This means code like obj.prop = undefined compiles successfully even though the intent of the optional marker was "the property may be absent", not "the property may be present with value undefined".

These are two meaningfully different states:

  • Object.hasOwn(obj, "prop") returns false when the property is absent.
  • Object.hasOwn(obj, "prop") returns true when prop is present with value undefined.

Enabling exactOptionalPropertyTypes forces TypeScript to distinguish between these states, catching a class of bugs where code assumes a property is absent but it is present with an undefined value, or vice versa.

The auto-fixer adds "exactOptionalPropertyTypes": true to compilerOptions.

โŒ Incorrectโ€‹

{
"compilerOptions": {
"strict": true
}
}

exactOptionalPropertyTypes is not enabled by strict and must be set explicitly.

โœ… Correctโ€‹

{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true
}
}

When not to use itโ€‹

Disable this rule when integrating with third-party libraries that rely on optional-as-undefined assignment patterns and cannot be updated.

Package documentationโ€‹

Rule catalog ID: R018

Further readingโ€‹