UNPKG

shapeit

Version:

Object validation tools for Javascript and, specially, Typescript

24 lines (23 loc) 1.03 kB
import { PrimitiveOrGuard, GuardType } from '../types/guards'; import { NonEmptyArray } from '../types/utils'; declare type GuardArray = NonEmptyArray<PrimitiveOrGuard<unknown>>; declare type _TupleToIntersection<T extends GuardArray, I extends number = 0> = `${I}` extends keyof T ? GuardType<T[I]> & (T extends [unknown, ...infer U] ? U extends GuardArray ? TupleToIntersection<U> : unknown : unknown) : unknown; declare type TupleToIntersection<T extends GuardArray> = _TupleToIntersection<T>; /** * Creates a guard for a intersection type from primitive names or other guards * * @example * const isValid = allOf( * looseShape({ a: 'string' }, false), * looseShape({ b: 'number' }, false) * ); * * if (isValid(input)) { * doSomethingWith(input); // input is typed as { a: string; b: number; } * } * else { * console.error(isValid.errors); // Errors found * } */ export default function allOf<T extends GuardArray>(...types: T): import("../types/guards").Guard<_TupleToIntersection<T, 0>>; export {};