isguard-ts
    Preparing search index...

    Function isLiteral

    • Creates a LiteralTypeGuard that checks that the value equals one of the values.

      Best practice: use satisfies on the result when passing multiple literal values.

      Type Parameters

      • const T extends readonly Literal[]

        Array of literal values

      Parameters

      • ...values: T

        The literal values to guard for

      Returns LiteralTypeGuard<T>

      A type guard for the provided values

      const isHello = isLiteral("Hello");

      isHello("Hello"); // true
      isHello("world"); // false
      const directions = ["up", "down", "left", "right"] as const;
      type Direction = (typeof directions)[number];

      const isDirection = isLiteral(...directions) satisfies TypeGuard<Direction>;

      isDirection("up"); // true