• P.optional(subpattern) takes a sub pattern and returns a pattern which matches if the key is undefined or if it is defined and the sub pattern matches its value.

    Read the documentation for P.optional on GitHub

    Type Parameters

    • input
    • const pattern extends undefined | null | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | {
          [k: string]: unknown;
      } | UnknownMatcher | PatternMatcher<input> | {
          readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]>
      } | readonly UnknownPattern[] | readonly [UnknownPattern, UnknownPattern] | readonly [UnknownPattern, UnknownPattern]

    Parameters

    Returns Chainable<OptionalP<input, pattern>, "optional">

    Example

    match(value)
    .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')