shapeit
Version:
Object validation tools for Javascript and, specially, Typescript
28 lines (27 loc) • 811 B
TypeScript
import { Guard } from '../types/guards';
import { NonEmptyArray } from '../types/utils';
declare type Cast<A, B> = A extends B ? A : B;
declare type Narrowable = string | number | bigint | boolean;
declare type Narrow<A> = A extends (...args: any) => any ? never : Cast<A, [] | (A extends Narrowable ? A : never) | ({
[K in keyof A]: Narrow<A[K]>;
})>;
/**
* Creates a guard that perfectly narrows a type.
*
* @example
* const is10 = sp.narrow(10);
*
* if (is10(input)) {
* input; // typed as 10
* }
*
* @example
* const isAorB = sp.narrow('a', 'b');
*
* if (isAorB(input)) {
* input; // typed as 'a' | 'b'
* }
*/
export default function narrow<T, U extends NonEmptyArray<Narrow<T>>>(...targets: U): Guard<U[number]>;
export declare function genType(target: unknown): string;
export {};