UNPKG

881 BTypeScriptView Raw
1import { Try } from '../Any/Try';
2import { Narrowable } from './_Internal';
3/**
4 * @hidden
5 */
6declare type NarrowRaw<A> = (A extends [] ? [] : never) | (A extends Narrowable ? A : never) | ({
7 [K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>;
8});
9/**
10 * Prevent type widening on generic function parameters
11 * @param A to narrow
12 * @returns `A`
13 * @example
14 * ```ts
15 * import {F} from 'ts-toolbelt'
16 *
17 * declare function foo<A extends any[]>(x: F.Narrow<A>): A;
18 * declare function bar<A extends object>(x: F.Narrow<A>): A;
19 *
20 * const test0 = foo(['e', 2, true, {f: ['g', ['h']]}])
21 * // `A` inferred : ['e', 2, true, {f: ['g']}]
22 *
23 * const test1 = bar({a: 1, b: 'c', d: ['e', 2, true, {f: ['g']}]})
24 * // `A` inferred : {a: 1, b: 'c', d: ['e', 2, true, {f: ['g']}]}
25 * ```
26 */
27declare type Narrow<A extends any> = Try<A, [], NarrowRaw<A>>;
28export { Narrow };