Skip to content

optionalOr

optionalOr is defaultTo with the source Spot wrapped in optional. It uses the fallback when the source is missing or its value is null or undefined.

// without optionalOr
const userName = defaultTo(optional(fetchUser.result.name), "Guest")
// with optionalOr
const userName = optionalOr(fetchUser.result.name, "Guest")
// without optionalOr
const userName = defaultTo(optional(fetchUser.result.name), defaultName.value)
// with optionalOr
const userName = optionalOr(fetchUser.result.name, defaultName.value)
  • spot: Spot<T> — the source Spot, treated as optional.
  • fallback: P | Spot<P> — a fallback value, or a Spot that provides it.

Spot<NonNullable<T> | P> — the source value when available and not null or undefined; otherwise, the fallback value.