isguard-ts
    Preparing search index...

    Type Alias TypeGuard<T, _U>

    Represents a type guard function that checks if a value is of type T. Type guards are functions that return a boolean indicating whether the input value matches the expected type. This type includes methods for chaining type guards to create more complex validations.

    const isPerson = isType<Person>({
    name: isString,
    age: isNumber
    });

    if (isPerson(someValue)) {
    // someValue is now typed as Person
    }
    type TypeGuard<in out T, in out _U extends ExactEqual<T> = ExactEqual<T>> = {
        and<I extends readonly unknown[]>(
            ...guards: TypeGuardTemplate<I>,
        ): IntersectionTypeGuard<[T, ...I[]]>;
        array(): ArrayTypeGuard<T>;
        indexRecord(): IndexRecordTypeGuard<T>;
        maybe(): MaybeTypeGuard<T>;
        optional(): OptionalTypeGuard<T>;
        or<I extends readonly unknown[]>(
            ...guards: TypeGuardTemplate<I>,
        ): UnionTypeGuard<[T, ...I[]]>;
        refine<R>(refinement: (value: T) => value is R): RefineTypeGuard<T, R>;
        set(): SetTypeGuard<T>;
        zod(): ZodType<T>;
        (value: unknown): value is T;
    }

    Type Parameters

    • in out T

      The type that this guard checks for

    • in out _U extends ExactEqual<T> = ExactEqual<T>
    • Parameters

      • value: unknown

        The value to check against the type guard

      Returns value is T

      true if the value is of type T, false otherwise

    Index

    Methods

    • Creates a zod schema equivalent to this type guard. You must have zod installed to use this feature.

      The supported versions of zod start with zod@3.20.0 and end with zod@5.0.0 (not included)

      Important

      The schema returned by .zod() might not exactly represent the guarded type in certain edge cases. For example: isNumber(NaN) returns true while z.number() marks NaN as invalid.

      The differences vary between zod versions, but these are the most common:

      • Non finite numbers (NaN, Infinity, -Infinity) are valid when using isguard-ts but invalid when using zod.
      • zod ignores symbol property keys while isguard-ts doesn't.

      Returns ZodType<T>

      const ZodNumber = isNumber.zod(); // same as z.number()

      type Person = {
      name: string;
      };

      const isPerson = isType<Person>({
      name: isString,
      });

      const ZodPerson = isPerson.zod(); // same as z.object({ name: z.string() })