Hyperlinkv0.8.0-beta.28

UndefinedOr

UndefinedOr.matchconsteffect/UndefinedOr.ts:51
<B, A, C = B>(options: {
  readonly onUndefined: LazyArg<B>
  readonly onDefined: (a: A) => C
}): (self: A | undefined) => B | C
<A, B, C = B>(
  self: A | undefined,
  options: {
    readonly onUndefined: LazyArg<B>
    readonly onDefined: (a: A) => C
  }
): B | C

Pattern matches on an A | undefined value, running onDefined when the value is present or evaluating onUndefined when the value is undefined.

When to use

Use when you need to turn an A | undefined into a non-optional result by handling both the defined and undefined branches in one expression.

pattern matchingmapgetOrThrowWith
export const match: {
  <B, A, C = B>(options: {
    readonly onUndefined: LazyArg<B>
    readonly onDefined: (a: A) => C
  }): (self: A | undefined) => B | C
  <A, B, C = B>(self: A | undefined, options: {
    readonly onUndefined: LazyArg<B>
    readonly onDefined: (a: A) => C
  }): B | C
} = dual(
  2,
  <A, B, C = B>(self: A | undefined, { onDefined, onUndefined }: {
    readonly onUndefined: LazyArg<B>
    readonly onDefined: (a: A) => C
  }): B | C => self === undefined ? onUndefined() : onDefined(self)
)