Optional type is a type that represents either a value or the absence of a value.

Optional types make absence explicit. Instead of allowing any value to be null, the type itself says that a value might not exist.

Examples include:

  • Option<T> in Rust
  • Maybe a in Haskell
  • Optional<T> in Java
  • T | undefined or T | null in TypeScript

Optional types are useful for:

  • Return values that may not exist
  • Avoiding unchecked null values
  • Making missing data visible in function signatures

An optional value usually has to be checked, matched, or unwrapped before the contained value can be used.

Anki

id: optional-type-definition deck: Computer Science::Data Types tags: data-types optional-type

Q: What is an optional type? A: An optional type represents either a value or the absence of a value.

id: optional-type-purpose deck: Computer Science::Data Types tags: data-types optional-type null

Q: Why are optional types useful? A: Optional types make absence explicit in the type system and can reduce unchecked null-related errors.