import type {IsNever} from './is-never.d.ts'; /** An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`. Use-cases: - You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If, 'is any', 'not any'>`. Note: - Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If` will return `'Y' | 'N'`. - Returns the else branch if the given type is `never`. For example, `If` will return `'N'`. @example ``` import {If} from 'type-fest'; type A = If; //=> 'yes' type B = If; //=> 'no' type C = If; //=> 'yes' | 'no' type D = If; //=> 'yes' | 'no' type E = If; //=> 'no' ``` @example ``` import {If, IsAny, IsNever} from 'type-fest'; type A = If, 'is any', 'not any'>; //=> 'not any' type B = If, 'is never', 'not never'>; //=> 'is never' ``` @example ``` import {If, IsEqual} from 'type-fest'; type IfEqual = If, IfBranch, ElseBranch>; type A = IfEqual; //=> 'equal' type B = IfEqual; //=> 'not equal' ``` Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example: @example ``` import type {If, IsEqual, StringRepeat} from 'type-fest'; type HundredZeroes = StringRepeat<'0', 100>; // The following implementation is not tail recursive type Includes = S extends `${infer First}${infer Rest}` ? If, 'found', Includes> : 'not found'; // Hence, instantiations with long strings will fail // @ts-expect-error type Fails = Includes; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Error: Type instantiation is excessively deep and possibly infinite. // However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive type IncludesWithoutIf = S extends `${infer First}${infer Rest}` ? IsEqual extends true ? 'found' : IncludesWithoutIf : 'not found'; // Now, instantiations with long strings will work type Works = IncludesWithoutIf; //=> 'not found' ``` @category Type Guard @category Utilities */ export type If = IsNever extends true ? ElseBranch : Type extends true ? IfBranch : ElseBranch; export {};