UNPKG

587 BTypeScriptView Raw
1/**
2 * Similar to [[Cast]] but with a custom fallback `Catch`. If it fails,
3 * it will enforce `Catch` instead of `A2`.
4 * @param A1 to check against
5 * @param A2 to try/test with
6 * @param Catch to fallback to if the test failed
7 * @returns `A1 | Catch`
8 * @example
9 * ```ts
10 * import {A} from 'ts-toolbelt'
11 *
12 * type test0 = A.Try<'42', string> // '42'
13 * type test1 = A.Try<'42', number> // never
14 * type test1 = A.Try<'42', number, 'tried'> // 'tried'
15 * ```
16 */
17export declare type Try<A1 extends any, A2 extends any, Catch = never> = A1 extends A2 ? A1 : Catch;