-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
is_never.ts
65 lines (63 loc) · 1.88 KB
/
is_never.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { $Any } from '../any/any.js'
import type { $SpecialType } from '../type_plus/$special_type.js'
import type { $InputOptions } from '../type_plus/branch/$input_options.js'
import type { $ResolveBranch } from '../type_plus/branch/$resolve_branch.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/$selection.js'
import type { $SelectionOptions } from '../type_plus/branch/$selection_options.js'
import type { $Unknown } from '../unknown/unknown.js'
import type { $NotNever } from './never.js'
/**
* 🎭 *predicate*
*
* Validate if `T` is `never`.
*
* @example
* ```ts
* type R = IsNever<never> // true
*
* type R = IsNever<1> // false
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is `never`, otherwise returns `$NotNever`.
*
* Filter normally returns `never` in the `$else` clause.
* But since we are checking for `never` here,
* we have to return `$NotNever` instead.
*
* @example
* ```ts
* type R = IsNever<never, { selection: 'filter' }> // never
*
* type R = IsNever<1, { selection: 'filter' }> // $NotNever
* ```
*
* 🔢 *customize*
*
* Use unique branch identifiers to allow precise processing of the result.
*
* @example
* ```ts
* type R = IsNever<never, $SelectionBranch> // $Then
* type R = IsNever<1, $SelectionBranch> // $Else
* ```
*/
export type IsNever<T, $O extends IsNever.$Options = {}> = $SpecialType<
T,
{
$any: $ResolveBranch<T, IsNever._O<$O>, [$Any, $Else]>
$unknown: $ResolveBranch<T, IsNever._O<$O>, [$Unknown, $Else]>
$never: $ResolveBranch<T, $O, [$Then]>
$else: $ResolveBranch<T, IsNever._O<$O>, [$Else]>
}
>
export namespace IsNever {
export type $Options = $SelectionOptions & $InputOptions<$Any | $Unknown>
export type $Branch = $SelectionBranch
export type _O<$O extends $Options> = '$else' extends keyof $O
? $O
: $O['selection'] extends 'filter'
? $O & { $else: $NotNever }
: $O
}