isguard-ts
    Preparing search index...

    Function isUnion

    • Creates a UnionTypeGuard that checks that the value matches any of the provided type guards.

      Best practice: use satisfies on the result of isUnion to ensure the result is of the expected union type.

      Can be shortened with TypeGuard.or.

      Type Parameters

      • T extends readonly unknown[]

        Array of types in the union

      Parameters

      Returns UnionTypeGuard<T>

      A type guard for the union of all the given type guards

      type A = { a: number };
      type B = { b: string };
      type C = A | B;

      const isA = isType<A>({ a: isNumber });
      const isB = isType<B>({ b: isString });

      const isC = isUnion(isA, isB) satisfies TypeGuard<C>; // or isA.or(isB)

      isC({ a: 1 }); // true
      isC({ b: "hello" }); // true
      isC({ a: 1, b: "hello" }); // true
      isC({}); // false