-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
is_not_string.ts
98 lines (94 loc) · 3.16 KB
/
is_not_string.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { NotAssignable } from '../predicates/not_assignable.js'
import type { $Equality } from '../type_plus/$equality.js'
import type { $MergeOptions } from '../type_plus/$merge_options.js'
import type { $ResolveOptions } from '../type_plus/$resolve_options.js'
import type { $SpecialType } from '../type_plus/$special_type.js'
import type { $Exact } from '../type_plus/branch/$exact.js'
import type { $IsDistributive } from '../type_plus/branch/$is_distributive.js'
import type { $ResolveBranch } from '../type_plus/branch/$resolve_branch.js'
import type { $Else, $Then } from '../type_plus/branch/$selection.js'
import type { $SelectionOptions } from '../type_plus/branch/$selection_options.js'
/**
* 🎭 *predicate*
*
* Validate if `T` is not `string` nor `string` literals.
*
* @example
* ```ts
* type R = IsNotString<string> // false
* type R = IsNotString<'a'> // false
*
* type R = IsNotString<never> // false
* type R = IsNotString<unknown> // false
* type R = IsNotString<string | boolean> // boolean
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is not `string` nor `string` literals, otherwise returns `never`.
*
* @example
* ```ts
* type R = IsNotString<string, { selection: 'filter' }> // never
* type R = IsNotString<'a', { selection: 'filter' }> // never
*
* type R = IsNotString<never, { selection: 'filter' }> // never
* type R = IsNotString<unknown, { selection: 'filter' }> // unknown
* type R = IsNotString<string | boolean, { selection: 'filter' }> // boolean
* ```
*
* 🔢 *customize*:
*
* Disable distribution of union types.
*
* ```ts
* type R = IsNotString<string | 1> // boolean
* type R = IsNotString<string | 1, { distributive: false }> // false
* ```
*
* 🔢 *customize*
*
* Use unique branch identifiers to allow precise processing of the result.
*
* @example
* ```ts
* type R = IsNotString<string, $IsNotString.$Branch> // $Else
* type R = IsNotString<bigint, $IsNotString.$Branch> // $Then
* ```
*/
export type IsNotString<T, $O extends IsNotString.$Options = {}> = $SpecialType<
T,
$MergeOptions<
$O,
{
$then: $ResolveBranch<T, $O, [$Then]>
$else: IsNotString.$<T, $O>
}
>
>
export namespace IsNotString {
export type $Options = $Equality.$Options & $Exact.$Options
export type $Branch<$O extends $Options = {}> = $Equality.$Branch<$O>
/**
* 🧰 *type util*
*
* Validate if `T` is not `string` nor `string` literals.
*
* This is a type util for building custom types.
* It does not check against special types.
*/
export type $<T, $O extends $UtilOptions> = $ResolveOptions<[$O['exact'], false]> extends true
? $IsDistributive<$O, { $then: _D<T, $O>; $else: _N<T, $O> }>
: NotAssignable.$<T, string, $O>
export type $UtilOptions = NotAssignable.$UtilOptions & $Exact.$Options
export type _D<T, $O extends $SelectionOptions> = T extends string & infer U
? U extends string
? $ResolveBranch<T, $O, [$Then]>
: $ResolveBranch<T, $O, [$Else]>
: $ResolveBranch<T, $O, [$Then]>
export type _N<T, $O extends $SelectionOptions> = [T] extends [string & infer U]
? U extends string
? $ResolveBranch<T, $O, [$Then]>
: $ResolveBranch<T, $O, [$Else]>
: $ResolveBranch<T, $O, [$Then]>
}