type-plus 7.4.0 → 7.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -92
- package/cjs/array/array.entries.d.ts +4 -3
- package/cjs/array/array.entries.d.ts.map +1 -1
- package/cjs/array/array_index.d.ts +4 -1
- package/cjs/array/array_index.d.ts.map +1 -1
- package/cjs/array/array_plus.common_prop_keys.d.ts +27 -0
- package/cjs/array/array_plus.common_prop_keys.d.ts.map +1 -0
- package/cjs/array/array_plus.common_prop_keys.js +3 -0
- package/cjs/array/array_plus.common_prop_keys.js.map +1 -0
- package/cjs/array/array_plus.d.ts +6 -3
- package/cjs/array/array_plus.d.ts.map +1 -1
- package/cjs/array/array_plus.drop_match.d.ts +4 -0
- package/cjs/array/array_plus.drop_match.d.ts.map +1 -0
- package/cjs/array/array_plus.drop_match.js +3 -0
- package/cjs/array/array_plus.drop_match.js.map +1 -0
- package/cjs/array/array_plus.element_match.d.ts +44 -0
- package/cjs/array/array_plus.element_match.d.ts.map +1 -0
- package/cjs/array/array_plus.element_match.js +3 -0
- package/cjs/array/array_plus.element_match.js.map +1 -0
- package/cjs/array/array_plus.find.d.ts +57 -0
- package/cjs/array/array_plus.find.d.ts.map +1 -0
- package/cjs/array/array_plus.find.js +3 -0
- package/cjs/array/array_plus.find.js.map +1 -0
- package/cjs/array/array_plus.split_at.d.ts +20 -15
- package/cjs/array/array_plus.split_at.d.ts.map +1 -1
- package/cjs/array/find_first.d.ts +62 -0
- package/cjs/array/find_first.d.ts.map +1 -0
- package/cjs/array/{array.find.js → find_first.js} +1 -1
- package/cjs/array/find_first.js.map +1 -0
- package/cjs/array/head.d.ts +18 -6
- package/cjs/array/head.d.ts.map +1 -1
- package/cjs/array/last.d.ts +17 -6
- package/cjs/array/last.d.ts.map +1 -1
- package/cjs/assertion/assert_type.d.ts +4 -1
- package/cjs/assertion/assert_type.d.ts.map +1 -1
- package/cjs/assertion/assert_type.js.map +1 -1
- package/cjs/index.d.ts +5 -2
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js.map +1 -1
- package/cjs/never/never_type.d.ts +11 -0
- package/cjs/never/never_type.d.ts.map +1 -1
- package/cjs/object/index.d.ts +0 -1
- package/cjs/object/index.d.ts.map +1 -1
- package/cjs/object/index.js.map +1 -1
- package/cjs/testing/test_type.d.ts +277 -276
- package/cjs/testing/test_type.d.ts.map +1 -1
- package/cjs/testing/test_type.js.map +1 -1
- package/cjs/tuple/common_prop_keys.d.ts +15 -10
- package/cjs/tuple/common_prop_keys.d.ts.map +1 -1
- package/cjs/tuple/drop.d.ts +45 -26
- package/cjs/tuple/drop.d.ts.map +1 -1
- package/cjs/tuple/drop.js.map +1 -1
- package/cjs/tuple/tuple_plus.common_prop_keys.d.ts +28 -0
- package/cjs/tuple/tuple_plus.common_prop_keys.d.ts.map +1 -0
- package/cjs/tuple/tuple_plus.common_prop_keys.js +3 -0
- package/cjs/tuple/tuple_plus.common_prop_keys.js.map +1 -0
- package/cjs/tuple/tuple_plus.d.ts +3 -0
- package/cjs/tuple/tuple_plus.d.ts.map +1 -1
- package/cjs/tuple/tuple_plus.drop_match.d.ts +6 -0
- package/cjs/tuple/tuple_plus.drop_match.d.ts.map +1 -0
- package/cjs/tuple/tuple_plus.drop_match.js +3 -0
- package/cjs/tuple/tuple_plus.drop_match.js.map +1 -0
- package/cjs/tuple/tuple_plus.filter.d.ts +2 -2
- package/cjs/tuple/tuple_plus.find.d.ts +64 -0
- package/cjs/tuple/tuple_plus.find.d.ts.map +1 -0
- package/cjs/tuple/tuple_plus.find.js +3 -0
- package/cjs/tuple/tuple_plus.find.js.map +1 -0
- package/cjs/tuple/tuple_plus.pad_start.d.ts +7 -6
- package/cjs/tuple/tuple_plus.pad_start.d.ts.map +1 -1
- package/cjs/tuple/tuple_type.d.ts +41 -23
- package/cjs/tuple/tuple_type.d.ts.map +1 -1
- package/cjs/union/union.d.ts +33 -0
- package/cjs/union/union.d.ts.map +1 -0
- package/cjs/union/union.js +3 -0
- package/cjs/union/union.js.map +1 -0
- package/cjs/unknown/unknown_type.d.ts +12 -0
- package/cjs/unknown/unknown_type.d.ts.map +1 -1
- package/cjs/utils/options.d.ts +10 -0
- package/cjs/utils/options.d.ts.map +1 -0
- package/cjs/utils/options.js +3 -0
- package/cjs/utils/options.js.map +1 -0
- package/esm/array/array.entries.d.ts +4 -3
- package/esm/array/array.entries.d.ts.map +1 -1
- package/esm/array/array_index.d.ts +4 -1
- package/esm/array/array_index.d.ts.map +1 -1
- package/esm/array/array_plus.common_prop_keys.d.ts +27 -0
- package/esm/array/array_plus.common_prop_keys.d.ts.map +1 -0
- package/esm/array/array_plus.common_prop_keys.js +2 -0
- package/esm/array/array_plus.common_prop_keys.js.map +1 -0
- package/esm/array/array_plus.d.ts +6 -3
- package/esm/array/array_plus.d.ts.map +1 -1
- package/esm/array/array_plus.drop_match.d.ts +4 -0
- package/esm/array/array_plus.drop_match.d.ts.map +1 -0
- package/esm/array/array_plus.drop_match.js +2 -0
- package/esm/array/array_plus.drop_match.js.map +1 -0
- package/esm/array/array_plus.element_match.d.ts +44 -0
- package/esm/array/array_plus.element_match.d.ts.map +1 -0
- package/esm/array/array_plus.element_match.js +2 -0
- package/esm/array/array_plus.element_match.js.map +1 -0
- package/esm/array/array_plus.find.d.ts +57 -0
- package/esm/array/array_plus.find.d.ts.map +1 -0
- package/esm/array/array_plus.find.js +2 -0
- package/esm/array/array_plus.find.js.map +1 -0
- package/esm/array/array_plus.split_at.d.ts +20 -15
- package/esm/array/array_plus.split_at.d.ts.map +1 -1
- package/esm/array/find_first.d.ts +62 -0
- package/esm/array/find_first.d.ts.map +1 -0
- package/esm/array/find_first.js +2 -0
- package/esm/array/find_first.js.map +1 -0
- package/esm/array/head.d.ts +18 -6
- package/esm/array/head.d.ts.map +1 -1
- package/esm/array/last.d.ts +17 -6
- package/esm/array/last.d.ts.map +1 -1
- package/esm/assertion/assert_type.d.ts +4 -1
- package/esm/assertion/assert_type.d.ts.map +1 -1
- package/esm/assertion/assert_type.js.map +1 -1
- package/esm/index.d.ts +5 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js.map +1 -1
- package/esm/never/never_type.d.ts +11 -0
- package/esm/never/never_type.d.ts.map +1 -1
- package/esm/object/index.d.ts +0 -1
- package/esm/object/index.d.ts.map +1 -1
- package/esm/object/index.js.map +1 -1
- package/esm/testing/test_type.d.ts +277 -276
- package/esm/testing/test_type.d.ts.map +1 -1
- package/esm/testing/test_type.js.map +1 -1
- package/esm/tuple/common_prop_keys.d.ts +15 -10
- package/esm/tuple/common_prop_keys.d.ts.map +1 -1
- package/esm/tuple/drop.d.ts +45 -26
- package/esm/tuple/drop.d.ts.map +1 -1
- package/esm/tuple/drop.js.map +1 -1
- package/esm/tuple/tuple_plus.common_prop_keys.d.ts +28 -0
- package/esm/tuple/tuple_plus.common_prop_keys.d.ts.map +1 -0
- package/esm/tuple/tuple_plus.common_prop_keys.js +2 -0
- package/esm/tuple/tuple_plus.common_prop_keys.js.map +1 -0
- package/esm/tuple/tuple_plus.d.ts +3 -0
- package/esm/tuple/tuple_plus.d.ts.map +1 -1
- package/esm/tuple/tuple_plus.drop_match.d.ts +6 -0
- package/esm/tuple/tuple_plus.drop_match.d.ts.map +1 -0
- package/esm/tuple/tuple_plus.drop_match.js +2 -0
- package/esm/tuple/tuple_plus.drop_match.js.map +1 -0
- package/esm/tuple/tuple_plus.filter.d.ts +2 -2
- package/esm/tuple/tuple_plus.find.d.ts +64 -0
- package/esm/tuple/tuple_plus.find.d.ts.map +1 -0
- package/esm/tuple/tuple_plus.find.js +2 -0
- package/esm/tuple/tuple_plus.find.js.map +1 -0
- package/esm/tuple/tuple_plus.pad_start.d.ts +7 -6
- package/esm/tuple/tuple_plus.pad_start.d.ts.map +1 -1
- package/esm/tuple/tuple_type.d.ts +41 -23
- package/esm/tuple/tuple_type.d.ts.map +1 -1
- package/esm/union/union.d.ts +33 -0
- package/esm/union/union.d.ts.map +1 -0
- package/esm/union/union.js +2 -0
- package/esm/union/union.js.map +1 -0
- package/esm/unknown/unknown_type.d.ts +12 -0
- package/esm/unknown/unknown_type.d.ts.map +1 -1
- package/esm/utils/options.d.ts +10 -0
- package/esm/utils/options.d.ts.map +1 -0
- package/esm/utils/options.js +2 -0
- package/esm/utils/options.js.map +1 -0
- package/package.json +12 -1
- package/ts/array/array.entries.ts +4 -2
- package/ts/array/array_index.ts +33 -23
- package/ts/array/array_plus.common_prop_keys.ts +35 -0
- package/ts/array/array_plus.drop_match.ts +16 -0
- package/ts/array/array_plus.element_match.ts +61 -0
- package/ts/array/array_plus.find.ts +71 -0
- package/ts/array/array_plus.split_at.ts +52 -23
- package/ts/array/array_plus.ts +6 -3
- package/ts/array/find_first.ts +71 -0
- package/ts/array/head.ts +28 -6
- package/ts/array/last.ts +26 -6
- package/ts/array/readme.md +152 -32
- package/ts/assertion/assert_type.ts +4 -1
- package/ts/assertion/readme.md +3 -2
- package/ts/index.ts +5 -5
- package/ts/never/never_type.ts +13 -0
- package/ts/object/index.ts +0 -1
- package/ts/testing/test_type.ts +279 -277
- package/ts/tuple/common_prop_keys.ts +18 -32
- package/ts/tuple/drop.ts +61 -60
- package/ts/tuple/readme.md +178 -39
- package/ts/tuple/tuple_plus.common_prop_keys.ts +47 -0
- package/ts/tuple/tuple_plus.drop_match.ts +20 -0
- package/ts/tuple/tuple_plus.filter.ts +2 -2
- package/ts/tuple/tuple_plus.find.ts +88 -0
- package/ts/tuple/tuple_plus.pad_start.ts +28 -25
- package/ts/tuple/tuple_plus.ts +3 -0
- package/ts/tuple/tuple_type.ts +67 -25
- package/ts/union/readme.md +81 -0
- package/ts/union/union.ts +37 -0
- package/ts/unknown/unknown_type.ts +13 -0
- package/ts/utils/options.ts +10 -0
- package/cjs/array/array.find.d.ts +0 -23
- package/cjs/array/array.find.d.ts.map +0 -1
- package/cjs/array/array.find.js.map +0 -1
- package/esm/array/array.find.d.ts +0 -23
- package/esm/array/array.find.d.ts.map +0 -1
- package/esm/array/array.find.js +0 -2
- package/esm/array/array.find.js.map +0 -1
- package/ts/array/array.find.ts +0 -34
|
@@ -17,39 +17,42 @@
|
|
|
17
17
|
* PadStart<[1, 2, 3], 5> // [unknown, unknown, 1, 2, 3]
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
export type PadStart<Tuple extends unknown[], MaxLength extends number, PadWith = unknown> =
|
|
20
|
+
export type PadStart<Tuple extends unknown[], MaxLength extends number, PadWith = unknown> = PadStart.Device<
|
|
21
21
|
Tuple,
|
|
22
22
|
MaxLength,
|
|
23
23
|
PadWith,
|
|
24
24
|
[]
|
|
25
25
|
>
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
27
|
+
export namespace PadStart {
|
|
28
|
+
export type Device<
|
|
29
|
+
Source extends unknown[],
|
|
30
|
+
MaxLength extends number,
|
|
31
|
+
PadWith,
|
|
32
|
+
Result extends unknown[]
|
|
33
|
+
> =
|
|
34
|
+
Result['length'] extends MaxLength
|
|
35
|
+
? (
|
|
36
|
+
Source extends []
|
|
37
|
+
? Result
|
|
38
|
+
: (
|
|
39
|
+
Source extends [...infer Head, infer Tail]
|
|
40
|
+
? (
|
|
41
|
+
[Tail, ...Result] extends infer R extends unknown[]
|
|
42
|
+
? Device<Head, R['length'], PadWith, R>
|
|
43
|
+
: never
|
|
44
|
+
)
|
|
42
45
|
: never
|
|
43
46
|
)
|
|
44
|
-
: never
|
|
45
47
|
)
|
|
46
|
-
)
|
|
47
|
-
: (
|
|
48
|
-
Source extends []
|
|
49
|
-
? PadStartDevice<Source, MaxLength, PadWith, [PadWith, ...Result]>
|
|
50
48
|
: (
|
|
51
|
-
Source extends [
|
|
52
|
-
?
|
|
53
|
-
:
|
|
49
|
+
Source extends []
|
|
50
|
+
? Device<Source, MaxLength, PadWith, [PadWith, ...Result]>
|
|
51
|
+
: (
|
|
52
|
+
Source extends [...infer Head, infer Tail]
|
|
53
|
+
? Device<Head, MaxLength, PadWith, [Tail, ...Result]>
|
|
54
|
+
: Source
|
|
55
|
+
)
|
|
54
56
|
)
|
|
55
|
-
|
|
57
|
+
|
|
58
|
+
}
|
package/ts/tuple/tuple_plus.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
+
export type { CommonPropKeys } from './tuple_plus.common_prop_keys.js'
|
|
2
|
+
export type { DropMatch } from './tuple_plus.drop_match.js'
|
|
1
3
|
export type { Filter } from './tuple_plus.filter.js'
|
|
4
|
+
export type { Find } from './tuple_plus.find.js'
|
|
2
5
|
export type { PadStart } from './tuple_plus.pad_start.js'
|
package/ts/tuple/tuple_type.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import type { IsNever } from '../never/never_type.js'
|
|
1
|
+
import type { IsNever, NeverType } from '../never/never_type.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 🌪️ *filter*
|
|
5
5
|
*
|
|
6
|
+
* Filter `T` to ensure it is a tuple, excluding array.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam Cases['never'] Return type when `T` is `never`. Default to `Else`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
6
11
|
* ```ts
|
|
7
12
|
* type R = TupleType<[]> // []
|
|
8
13
|
* type R = TupleType<[1]> // [1]
|
|
@@ -13,52 +18,89 @@ import type { IsNever } from '../never/never_type.js'
|
|
|
13
18
|
* type R = TupleType<unknown> // never
|
|
14
19
|
* ```
|
|
15
20
|
*/
|
|
16
|
-
export type TupleType<
|
|
21
|
+
export type TupleType<
|
|
22
|
+
T,
|
|
23
|
+
Then = T,
|
|
24
|
+
Else = never,
|
|
25
|
+
Options extends TupleType.Options = TupleType.DefaultOptions<Else>
|
|
26
|
+
> = IsNever<
|
|
17
27
|
T,
|
|
18
|
-
|
|
28
|
+
Options['caseNever'],
|
|
19
29
|
[T] extends [any[]] ? (number extends T['length'] ? Else : Then) : Else
|
|
20
30
|
>
|
|
21
31
|
|
|
32
|
+
export namespace TupleType {
|
|
33
|
+
export interface Options extends NeverType.Options {
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface DefaultOptions<Else> {
|
|
37
|
+
caseNever: Else
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
22
42
|
/**
|
|
23
|
-
*
|
|
43
|
+
* 🎭 *validate*
|
|
44
|
+
*
|
|
45
|
+
* Validate that `T` is a tuple, excluding array.
|
|
24
46
|
*
|
|
25
47
|
* ```ts
|
|
26
|
-
* type R =
|
|
27
|
-
* type R = NotTupleType<[1]> // never
|
|
48
|
+
* type R = IsTuple<[]> // true
|
|
28
49
|
*
|
|
29
|
-
* type R =
|
|
30
|
-
* type R =
|
|
31
|
-
* type R =
|
|
32
|
-
* type R =
|
|
50
|
+
* type R = IsTuple<number[]> // false
|
|
51
|
+
* type R = IsTuple<string> // false
|
|
52
|
+
* type R = IsTuple<never> // false
|
|
53
|
+
* type R = IsTuple<unknown> // false
|
|
33
54
|
* ```
|
|
34
55
|
*/
|
|
35
|
-
export type
|
|
56
|
+
export type IsTuple<
|
|
57
|
+
T,
|
|
58
|
+
Then = true,
|
|
59
|
+
Else = false,
|
|
60
|
+
Options extends TupleType.Options = TupleType.DefaultOptions<Else>
|
|
61
|
+
> = TupleType<T, Then, Else, Options>
|
|
62
|
+
|
|
36
63
|
|
|
37
64
|
/**
|
|
38
|
-
*
|
|
65
|
+
* 🌪️ *filter*
|
|
66
|
+
*
|
|
67
|
+
* Filter `T` to ensure it is not an tuple, excluding array.
|
|
39
68
|
*
|
|
40
69
|
* ```ts
|
|
41
|
-
* type R =
|
|
70
|
+
* type R = NotTupleType<[]> // never
|
|
71
|
+
* type R = NotTupleType<[1]> // never
|
|
42
72
|
*
|
|
43
|
-
* type R =
|
|
44
|
-
* type R =
|
|
45
|
-
* type R =
|
|
46
|
-
* type R =
|
|
73
|
+
* type R = NotTupleType<number[]> // number[]
|
|
74
|
+
* type R = NotTupleType<string> // string
|
|
75
|
+
* type R = NotTupleType<never> // never
|
|
76
|
+
* type R = NotTupleType<unknown> // unknown
|
|
47
77
|
* ```
|
|
48
78
|
*/
|
|
49
|
-
export type
|
|
79
|
+
export type NotTupleType<
|
|
80
|
+
T,
|
|
81
|
+
Then = T,
|
|
82
|
+
Else = never,
|
|
83
|
+
Options extends TupleType.Options = TupleType.DefaultOptions<Then>
|
|
84
|
+
> = TupleType<T, Else, Then, Options>
|
|
50
85
|
|
|
51
86
|
/**
|
|
52
|
-
*
|
|
87
|
+
* 🎭 *validate*
|
|
88
|
+
*
|
|
89
|
+
* Validate that `T` is not a tuple, excluding array.
|
|
53
90
|
*
|
|
54
91
|
* ```ts
|
|
55
92
|
* type R = IsNotTuple<[]> // false
|
|
56
93
|
* type R = IsNotTuple<[1]> // false
|
|
57
94
|
*
|
|
58
|
-
* type R = IsNotTuple<number[]> //
|
|
59
|
-
* type R = IsNotTuple<string> //
|
|
60
|
-
* type R = IsNotTuple<never> //
|
|
61
|
-
* type R = IsNotTuple<unknown> //
|
|
95
|
+
* type R = IsNotTuple<number[]> // true
|
|
96
|
+
* type R = IsNotTuple<string> // true
|
|
97
|
+
* type R = IsNotTuple<never> // true
|
|
98
|
+
* type R = IsNotTuple<unknown> // true
|
|
62
99
|
* ```
|
|
63
100
|
*/
|
|
64
|
-
export type IsNotTuple<
|
|
101
|
+
export type IsNotTuple<
|
|
102
|
+
T,
|
|
103
|
+
Then = true,
|
|
104
|
+
Else = false,
|
|
105
|
+
Options extends TupleType.Options = TupleType.DefaultOptions<Then>
|
|
106
|
+
> = TupleType<T, Else, Then, Options>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# union
|
|
2
|
+
|
|
3
|
+
Union type is a type that combines multiple types as union.
|
|
4
|
+
|
|
5
|
+
A value belongs to a union type if it belongs to one (or more) of its member types.
|
|
6
|
+
|
|
7
|
+
For example:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type U1 = string | number
|
|
11
|
+
|
|
12
|
+
const u1: U1 = 'a'
|
|
13
|
+
const u2: U1 = 1
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
As a mental shortcut, you can think of it as an *or* operator.
|
|
17
|
+
But it is better to think of it as union of category instead.
|
|
18
|
+
|
|
19
|
+
## Union Members
|
|
20
|
+
|
|
21
|
+
Each member of a union type is referred to as the union's member.
|
|
22
|
+
|
|
23
|
+
It is useful to understand the relationship between members of a union type.
|
|
24
|
+
|
|
25
|
+
In this context, we are going to consider types as sets,
|
|
26
|
+
instead of categories, to make it easier to understand.
|
|
27
|
+
|
|
28
|
+
Given two union members of a union,
|
|
29
|
+
their relationship can be describe as "A x B" where "x" is:
|
|
30
|
+
|
|
31
|
+
- disjoint: there is no overlap between A and B (e.g. `number | string`).
|
|
32
|
+
- overlap: there are some overlap between A and B (e.g. `1 | 2` and `2 | 3` has overlap `2`)
|
|
33
|
+
- subset/superset: A is a subset of B (or B is a superset of A) when every element in A is also in B (e.g. `1 | 2` is a subset of `number`)
|
|
34
|
+
|
|
35
|
+
The union type will consider these relations when it is declared.
|
|
36
|
+
|
|
37
|
+
There are other relations between members of a union type,
|
|
38
|
+
which will affect how the union type behaves.
|
|
39
|
+
|
|
40
|
+
For example:
|
|
41
|
+
|
|
42
|
+
- property overlap: e.g. `{ a: 1 } | { a: 2 }`
|
|
43
|
+
- property overlap with extension: e.g. `{ a: 1, b?: 2 } | { a: 1, c?: 3 }`
|
|
44
|
+
|
|
45
|
+
The names of these relations are not properly defined and may change in the future.
|
|
46
|
+
|
|
47
|
+
## [UnionType](./union.ts#l13)
|
|
48
|
+
|
|
49
|
+
`UnionType<T, Then = T, Else = never>`
|
|
50
|
+
|
|
51
|
+
🌪️ *filter*
|
|
52
|
+
|
|
53
|
+
Filter the type `T` to ensure it is a union.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import type { UnionType } from 'type-plus'
|
|
57
|
+
|
|
58
|
+
type R = UnionType<1 | 2> // 1 | 2
|
|
59
|
+
type R = UnionType<boolean> // boolean
|
|
60
|
+
type R = UnionType<number> // never
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## [IsUnion](./union.ts#l30)
|
|
64
|
+
|
|
65
|
+
🎭 *validate*
|
|
66
|
+
|
|
67
|
+
Validate that `T` is a union.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import type { IsUnion } from 'type-plus'
|
|
71
|
+
|
|
72
|
+
type R = IsUnion<1 | 2> // true
|
|
73
|
+
type R = IsUnion<boolean> // true
|
|
74
|
+
type R = IsUnion<number> // false
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Reference
|
|
78
|
+
|
|
79
|
+
- [handbook]
|
|
80
|
+
|
|
81
|
+
[handbook]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🌪️ *filter*
|
|
3
|
+
*
|
|
4
|
+
* Filter the type `T` to ensure it is a union.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type R = IsUnion<'a' | 'b'> // 'a' | 'b'
|
|
9
|
+
* type R = IsUnion<boolean> // boolean
|
|
10
|
+
* type R = IsUnion<number> // never
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export type UnionType<T, Then = T, Else = never> = UnionType.Device<T, Then, Else>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 🎭 *validate*
|
|
17
|
+
*
|
|
18
|
+
* Validate the type `T` is a union.
|
|
19
|
+
*
|
|
20
|
+
* @author Nurbol Alpysbayev
|
|
21
|
+
* @see https://stackoverflow.com/questions/53953814/typescript-check-if-a-type-is-a-union
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* type R = IsUnion<'a' | 'b'> // true
|
|
26
|
+
* type R = IsUnion<boolean> // true
|
|
27
|
+
* type R = IsUnion<number> // false
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export type IsUnion<T, Then = true, Else = false> = UnionType.Device<T, Then, Else>
|
|
31
|
+
|
|
32
|
+
export namespace UnionType {
|
|
33
|
+
export type Device<T, Then, Else, U = T> =
|
|
34
|
+
(T extends unknown ? (U extends T ? 1 : 2) : never) extends 1
|
|
35
|
+
? Else
|
|
36
|
+
: Then
|
|
37
|
+
}
|
|
@@ -53,3 +53,16 @@ export type NotUnknownType<T, Then = T, Else = never> = UnknownType<T, Else, The
|
|
|
53
53
|
* ```
|
|
54
54
|
*/
|
|
55
55
|
export type IsNotUnknown<T, Then = true, Else = false> = UnknownType<T, Else, Then>
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 🌪️ *filter*
|
|
59
|
+
*
|
|
60
|
+
* Returns `T` if `T` is not `unknown`, otherwise `Else`.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* type R = NotUnknownOr<number, string> // number
|
|
65
|
+
* type R = NotUnknownOr<unknown, number> // number
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export type NotUnknownOr<T, Else> = UnknownType<T, Else, T>
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { TupleType } from '../tuple/tuple_type.js';
|
|
2
|
-
/**
|
|
3
|
-
* 🦴 *utilities*
|
|
4
|
-
*
|
|
5
|
-
* Gets the first type in the array or tuple that matches the `Criteria`.
|
|
6
|
-
*
|
|
7
|
-
* If the `Criteria` is not met, it will return `never'.
|
|
8
|
-
*
|
|
9
|
-
* For `Array<T>`, it will return `T | undefined` if `T` satisfies `Criteria`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* ArrayPlus.Find<Array<1 | 2 | 'x'>, number> // 1 | 2 | undefined
|
|
14
|
-
*
|
|
15
|
-
* ArrayPlus.Find<[true, 1, 'x', 3], string> // 'x'
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export type FindFirst<A extends unknown[], Criteria> = TupleType<A, A['length'] extends 0 ? never : A extends [infer Head, ...infer Tail] ? Head extends Criteria ? Head : FindFirst<Tail, Criteria> : never, A extends Array<infer T> ? (T extends Criteria ? T | undefined : never) : never>;
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated use FindFirst
|
|
21
|
-
*/
|
|
22
|
-
export type First<A extends any[], Criteria> = FindFirst<A, Criteria>;
|
|
23
|
-
//# sourceMappingURL=array.find.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"array.find.d.ts","sourceRoot":"","sources":["../../ts/array/array.find.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAEvD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,QAAQ,IAAI,SAAS,CAC/D,CAAC,EACD,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAClB,KAAK,GACL,CAAC,SAAS,CAAC,MAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,GACrC,IAAI,SAAS,QAAQ,GACpB,IAAI,GACJ,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,GAC1B,KAAK,EACR,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,KAAK,CAC/E,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,QAAQ,IAAI,SAAS,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"array.find.js","sourceRoot":"","sources":["../../ts/array/array.find.ts"],"names":[],"mappings":""}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { TupleType } from '../tuple/tuple_type.js';
|
|
2
|
-
/**
|
|
3
|
-
* 🦴 *utilities*
|
|
4
|
-
*
|
|
5
|
-
* Gets the first type in the array or tuple that matches the `Criteria`.
|
|
6
|
-
*
|
|
7
|
-
* If the `Criteria` is not met, it will return `never'.
|
|
8
|
-
*
|
|
9
|
-
* For `Array<T>`, it will return `T | undefined` if `T` satisfies `Criteria`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* ArrayPlus.Find<Array<1 | 2 | 'x'>, number> // 1 | 2 | undefined
|
|
14
|
-
*
|
|
15
|
-
* ArrayPlus.Find<[true, 1, 'x', 3], string> // 'x'
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export type FindFirst<A extends unknown[], Criteria> = TupleType<A, A['length'] extends 0 ? never : A extends [infer Head, ...infer Tail] ? Head extends Criteria ? Head : FindFirst<Tail, Criteria> : never, A extends Array<infer T> ? (T extends Criteria ? T | undefined : never) : never>;
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated use FindFirst
|
|
21
|
-
*/
|
|
22
|
-
export type First<A extends any[], Criteria> = FindFirst<A, Criteria>;
|
|
23
|
-
//# sourceMappingURL=array.find.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"array.find.d.ts","sourceRoot":"","sources":["../../ts/array/array.find.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAEvD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,QAAQ,IAAI,SAAS,CAC/D,CAAC,EACD,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAClB,KAAK,GACL,CAAC,SAAS,CAAC,MAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,GACrC,IAAI,SAAS,QAAQ,GACpB,IAAI,GACJ,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,GAC1B,KAAK,EACR,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,KAAK,CAC/E,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,QAAQ,IAAI,SAAS,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA"}
|
package/esm/array/array.find.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"array.find.js","sourceRoot":"","sources":["../../ts/array/array.find.ts"],"names":[],"mappings":""}
|
package/ts/array/array.find.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { TupleType } from '../tuple/tuple_type.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 🦴 *utilities*
|
|
5
|
-
*
|
|
6
|
-
* Gets the first type in the array or tuple that matches the `Criteria`.
|
|
7
|
-
*
|
|
8
|
-
* If the `Criteria` is not met, it will return `never'.
|
|
9
|
-
*
|
|
10
|
-
* For `Array<T>`, it will return `T | undefined` if `T` satisfies `Criteria`.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* ArrayPlus.Find<Array<1 | 2 | 'x'>, number> // 1 | 2 | undefined
|
|
15
|
-
*
|
|
16
|
-
* ArrayPlus.Find<[true, 1, 'x', 3], string> // 'x'
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export type FindFirst<A extends unknown[], Criteria> = TupleType<
|
|
20
|
-
A,
|
|
21
|
-
A['length'] extends 0
|
|
22
|
-
? never
|
|
23
|
-
: A extends [infer Head, ...infer Tail]
|
|
24
|
-
? Head extends Criteria
|
|
25
|
-
? Head
|
|
26
|
-
: FindFirst<Tail, Criteria>
|
|
27
|
-
: never,
|
|
28
|
-
A extends Array<infer T> ? (T extends Criteria ? T | undefined : never) : never
|
|
29
|
-
>
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @deprecated use FindFirst
|
|
33
|
-
*/
|
|
34
|
-
export type First<A extends any[], Criteria> = FindFirst<A, Criteria>
|