foundation

Optional and readonly fields

Represent partial data, immutable contracts, patch objects, and exact optional property semantics.

Optional fields (`?`) describe properties that may be absent. `readonly` restricts assignment through that type view; it does not deep-freeze runtime objects. These modifiers appear in DTOs, patch payloads, React props, and configuration.

With `exactOptionalPropertyTypes`, optional is not always the same as `T | undefined`. `Partial<T>` builds patch shapes; `Readonly<T>` makes top-level properties readonly.

					type Patch = Partial<Pick<User, 'name' | 'email'>>;
				

On interviews: distinguish absent from undefined, explain shallow immutability, and mention `Readonly` versus runtime freezing.

Common pitfalls: mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus safety or expressiveness versus maintainability. Checklist:

  • Distinguish absent from undefined.
  • Know readonly is compile-time and shallow.
  • Use Partial for patch shapes.