Skip to content

hasInformation

Type guard based on the literal information stored in the Either. Lets you precisely target one or many business cases without extra introspection.

Interactive example

Syntax

typescript
function hasInformation<
  const GenericInput extends unknown,
  GenericInformation extends (
    GenericInput extends Either
      ? ReturnType<typeof informationKind.getValue<GenericInput>>
      : never
  ),
>(
  input: GenericInput,
  information: GenericInformation | GenericInformation[],
): input is Extract<
  GenericInput,
  Kind<typeof informationKind.definition, GenericInformation>
>;

function hasInformation<
  const GenericInput extends unknown,
  GenericInformation extends (
    GenericInput extends Either
      ? ReturnType<typeof informationKind.getValue<GenericInput>>
      : never
  ),
>(
  information: GenericInformation | GenericInformation[],
): (input: GenericInput) => input is Extract<
  GenericInput,
  Kind<typeof informationKind.definition, GenericInformation>
>;

Parameters

  • information: Expected literal information, or an array of literal informations.
  • input (optional in the curried signature): Value on which to perform the check.

Return value

A boolean, but mostly a type guard: when the result is true, TypeScript knows the Either carries one of the requested informations (Right or Left).

Use cases

  • Differentiate several errors/successes within the same Either.
  • Match multiple business informations in a single guard.
  • Chain with E.whenHasInformation to trigger a targeted action.

See also

Released under the MIT license.