ts-data-forge
    Preparing search index...

    Function match

    • Type-safe pattern matching function for string-based discriminated unions.

      Provides compile-time guarantees for exhaustive case handling when working with literal string unions. Automatically enforces completeness checking when all cases are covered, and requires a default value when cases are incomplete.

      • Exhaustive Matching: When all cases of a literal union are handled, no default value is needed
      • Partial Matching: When cases are incomplete or working with general string types, a default value is required
      • Type Safety: Prevents extra cases and ensures only valid keys are used
      • Strict Property Checking: Rejects objects with unexpected properties

      Type Parameters

      • const Case extends string
      • const R extends ReadonlyRecord<Case, unknown>

      Parameters

      • target: Case

        The value to match against

      • cases: StrictPropertyCheck<R, Case>

        Object mapping possible values to their corresponding results

      Returns R[Case]

      The matched result or default value

      type Status = 'draft' | 'review' | 'published';

      const status: Status = 'draft';

      const message = match<
      Status,
      { draft: string; review: string; published: string }
      >(status, {
      draft: 'Work in progress',
      review: 'Awaiting feedback',
      published: 'Complete',
      });

      assert(message === 'Work in progress');
    • Type-safe pattern matching function for string-based discriminated unions.

      Provides compile-time guarantees for exhaustive case handling when working with literal string unions. Automatically enforces completeness checking when all cases are covered, and requires a default value when cases are incomplete.

      • Exhaustive Matching: When all cases of a literal union are handled, no default value is needed
      • Partial Matching: When cases are incomplete or working with general string types, a default value is required
      • Type Safety: Prevents extra cases and ensures only valid keys are used
      • Strict Property Checking: Rejects objects with unexpected properties

      Type Parameters

      • const Case extends string
      • const R extends UnknownRecord
      • const D

      Parameters

      • target: Case

        The value to match against

      • cases: StrictPropertyCheck<R, Case>

        Object mapping possible values to their corresponding results

      • defaultValue: IsLiteralUnionFullyCovered<Case, R> extends true ? never : D

        Fallback value (required when not all cases are covered)

      Returns D | ValueOf<R>

      The matched result or default value

      type Status = 'draft' | 'review' | 'published';

      const status: Status = 'draft';

      const message = match<
      Status,
      { draft: string; review: string; published: string }
      >(status, {
      draft: 'Work in progress',
      review: 'Awaiting feedback',
      published: 'Complete',
      });

      assert(message === 'Work in progress');