UNPKG

bguard

Version:

**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.

15 lines (12 loc) 1.01 kB
import { WithBGuardType, WithUndefined, WithNull, ExtractFromBGuardType, WithArray, ExtractFromArray, WithObject } from './commonTypes.mjs'; type ResolveNullish<T, Y> = T extends WithUndefined<WithNull<unknown>> ? Y | null | undefined : T extends WithUndefined<unknown> ? Y | undefined : T extends WithNull<unknown> ? Y | null : Y; type InferType<T> = T extends WithBGuardType<unknown, unknown> ? ResolveNullish<T, ExtractFromBGuardType<T>> : T extends WithArray<unknown, unknown> ? ResolveNullish<T, InferType<ExtractFromArray<T>>[]> : T extends WithObject<unknown, unknown> ? ResolveNullish<T, ExtractFromObject<T>> : unknown; type Merge<T> = T extends infer U ? { [K in keyof U]: U[K]; } : never; type ExtractFromObject<T> = T extends WithObject<unknown, infer X> ? Merge<{ [K in keyof X as X[K] extends WithUndefined<unknown> ? never : K]: InferType<X[K]>; } & { [K in keyof X as X[K] extends WithUndefined<unknown> ? K : never]?: InferType<X[K]>; }> : unknown; export type { InferType };