UNPKG

simple-tg

Version:
44 lines 2.2 kB
declare type Primitives = { string: string; number: number; boolean: boolean; bigint: bigint; function: Function; symbol: symbol; object: object | null; undefined: undefined; }; export declare type TypeGuard<T> = (x: any) => x is T; /** * A matcher for a type, which is either a single primitive name, a list of primitive names, or a type guard function */ export declare type Matcher<P extends PrimitiveName = PrimitiveName, T = any> = P | P[] | TypeGuard<T>; declare type Flatten<T> = T extends Array<infer E> ? E : T; declare type MatchedType<T extends Matcher> = T extends TypeGuard<infer E> ? E : Primitive<Extract<Flatten<T>, PrimitiveName>>; export declare type Primitive<K extends keyof Primitives = keyof Primitives> = Primitives[K]; export declare type PrimitiveName<V extends Primitive = Primitive> = V extends string ? 'string' : V extends number ? 'number' : V extends boolean ? 'boolean' : V extends bigint ? 'bigint' : V extends Function ? 'function' : V extends symbol ? 'symbol' : V extends object | null ? 'object' : V extends undefined ? 'undefined' : never; export declare function guard<T>(f: (x: any) => boolean): TypeGuard<T>; /** * Return a type guard function checking whether a value is the type defined by the matcher * @param matcher */ export declare function is<P extends PrimitiveName, T extends Primitives[P]>(matcher: Matcher<P, T>): TypeGuard<T>; /** * Return a type guard function checking whether a value is an array where each element matches the matcher * @param matcher */ export declare function isArray<P extends PrimitiveName, T extends Primitives[P]>(matcher: Matcher<P, T>): TypeGuard<T[]>; /** * Return a type guard function checking whether a value is an object with the entries defined by the given map of property names to matchers * @param matchObj */ export declare function isObject<M extends Record<any, Matcher>>(matchObj: M): TypeGuard<{ [K in keyof M]: MatchedType<M[K]>; }>; /** * Return a type guard function checking whether a value equals one of the given values * @param values */ export declare function isIn<T>(values: T[]): TypeGuard<T>; export {}; //# sourceMappingURL=index.d.ts.map