type-plus 7.3.0 → 7.4.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 +131 -156
- package/cjs/any/any_type.d.ts +8 -8
- package/cjs/any/any_type.d.ts.map +1 -1
- package/cjs/array/array.at.d.ts +3 -1
- package/cjs/array/array.at.d.ts.map +1 -1
- package/cjs/array/array.find.d.ts +3 -1
- package/cjs/array/array.find.d.ts.map +1 -1
- package/cjs/array/array.find_last.d.ts +3 -1
- package/cjs/array/array.find_last.d.ts.map +1 -1
- package/cjs/array/array.some.d.ts +1 -1
- package/cjs/array/array.some.d.ts.map +1 -1
- package/cjs/array/{array.concat.d.ts → array_plus.concat.d.ts} +4 -1
- package/cjs/array/array_plus.concat.d.ts.map +1 -0
- package/cjs/array/{array.concat.js → array_plus.concat.js} +1 -1
- package/cjs/array/array_plus.concat.js.map +1 -0
- package/cjs/array/array_plus.d.ts +3 -2
- package/cjs/array/array_plus.d.ts.map +1 -1
- package/cjs/array/array_plus.filter.d.ts +2 -0
- package/cjs/array/array_plus.filter.d.ts.map +1 -0
- package/cjs/array/array_plus.filter.js +3 -0
- package/cjs/array/array_plus.filter.js.map +1 -0
- package/cjs/array/array_plus.pad_start.d.ts +5 -0
- package/cjs/array/array_plus.pad_start.d.ts.map +1 -0
- package/cjs/array/array_plus.pad_start.js +3 -0
- package/cjs/array/array_plus.pad_start.js.map +1 -0
- package/cjs/array/array_type.d.ts +30 -12
- package/cjs/array/array_type.d.ts.map +1 -1
- package/cjs/array/filter.d.ts +24 -4
- package/cjs/array/filter.d.ts.map +1 -1
- package/cjs/array/head.d.ts +18 -1
- package/cjs/array/head.d.ts.map +1 -1
- package/cjs/array/intersect_of_props.d.ts +5 -3
- package/cjs/array/intersect_of_props.d.ts.map +1 -1
- package/cjs/array/last.d.ts +16 -2
- package/cjs/array/last.d.ts.map +1 -1
- package/cjs/array/literal_array.d.ts +5 -0
- package/cjs/array/literal_array.d.ts.map +1 -1
- package/cjs/array/literal_array.js +5 -0
- package/cjs/array/literal_array.js.map +1 -1
- package/cjs/array/pad_start.d.ts +5 -5
- package/cjs/array/pad_start.d.ts.map +1 -1
- package/cjs/array/reduce_while.d.ts +6 -0
- package/cjs/array/reduce_while.d.ts.map +1 -1
- package/cjs/array/reduce_while.js +6 -0
- package/cjs/array/reduce_while.js.map +1 -1
- package/cjs/array/reverse.d.ts +5 -0
- package/cjs/array/reverse.d.ts.map +1 -1
- package/cjs/array/union_of_props.d.ts +1 -1
- package/cjs/array/union_of_props.d.ts.map +1 -1
- package/cjs/functional/compose.d.ts +2 -1
- package/cjs/functional/compose.d.ts.map +1 -1
- package/cjs/functional/compose.js.map +1 -1
- package/cjs/functional/context.d.ts.map +1 -1
- package/cjs/index.d.ts +19 -3
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +9 -3
- package/cjs/index.js.map +1 -1
- package/cjs/math/numeric_struct.d.ts +1 -1
- package/cjs/math/numeric_struct.d.ts.map +1 -1
- package/cjs/mix_types/any_or_never_type.d.ts +18 -10
- package/cjs/mix_types/any_or_never_type.d.ts.map +1 -1
- package/cjs/nodejs/isNodeError.d.ts.map +1 -1
- package/cjs/nominal/brand.d.ts.map +1 -1
- package/cjs/nominal/flavor.d.ts.map +1 -1
- package/cjs/testing/test_type.d.ts +44 -1
- package/cjs/testing/test_type.d.ts.map +1 -1
- package/cjs/tuple/common_prop_keys.d.ts +29 -0
- package/cjs/tuple/common_prop_keys.d.ts.map +1 -0
- package/cjs/tuple/common_prop_keys.js.map +1 -0
- package/cjs/tuple/create_tuple.d.ts.map +1 -1
- package/cjs/tuple/drop.d.ts +72 -0
- package/cjs/tuple/drop.d.ts.map +1 -0
- package/cjs/{array → tuple}/drop.js +3 -1
- package/cjs/tuple/drop.js.map +1 -0
- package/{esm/array → cjs/tuple}/tail.d.ts +1 -1
- package/cjs/{array → tuple}/tail.d.ts.map +1 -1
- package/cjs/{array → tuple}/tail.js.map +1 -1
- package/cjs/tuple/tuple_plus.d.ts +2 -1
- package/cjs/tuple/tuple_plus.d.ts.map +1 -1
- package/cjs/tuple/tuple_plus.filter.d.ts +13 -0
- package/cjs/tuple/tuple_plus.filter.d.ts.map +1 -0
- package/cjs/tuple/tuple_plus.filter.js +3 -0
- package/cjs/tuple/tuple_plus.filter.js.map +1 -0
- package/cjs/tuple/tuple_plus.js +0 -15
- package/cjs/tuple/tuple_plus.js.map +1 -1
- package/cjs/tuple/tuple_plus.pad_start.d.ts +8 -3
- package/cjs/tuple/tuple_plus.pad_start.d.ts.map +1 -1
- package/esm/any/any_type.d.ts +8 -8
- package/esm/any/any_type.d.ts.map +1 -1
- package/esm/array/array.at.d.ts +3 -1
- package/esm/array/array.at.d.ts.map +1 -1
- package/esm/array/array.find.d.ts +3 -1
- package/esm/array/array.find.d.ts.map +1 -1
- package/esm/array/array.find_last.d.ts +3 -1
- package/esm/array/array.find_last.d.ts.map +1 -1
- package/esm/array/array.some.d.ts +1 -1
- package/esm/array/array.some.d.ts.map +1 -1
- package/esm/array/{array.concat.d.ts → array_plus.concat.d.ts} +4 -1
- package/esm/array/array_plus.concat.d.ts.map +1 -0
- package/esm/array/array_plus.concat.js +2 -0
- package/esm/array/array_plus.concat.js.map +1 -0
- package/esm/array/array_plus.d.ts +3 -2
- package/esm/array/array_plus.d.ts.map +1 -1
- package/esm/array/array_plus.filter.d.ts +2 -0
- package/esm/array/array_plus.filter.d.ts.map +1 -0
- package/esm/array/array_plus.filter.js +2 -0
- package/esm/array/array_plus.filter.js.map +1 -0
- package/esm/array/array_plus.pad_start.d.ts +5 -0
- package/esm/array/array_plus.pad_start.d.ts.map +1 -0
- package/esm/array/array_plus.pad_start.js +2 -0
- package/esm/array/array_plus.pad_start.js.map +1 -0
- package/esm/array/array_type.d.ts +30 -12
- package/esm/array/array_type.d.ts.map +1 -1
- package/esm/array/filter.d.ts +24 -4
- package/esm/array/filter.d.ts.map +1 -1
- package/esm/array/head.d.ts +18 -1
- package/esm/array/head.d.ts.map +1 -1
- package/esm/array/intersect_of_props.d.ts +5 -3
- package/esm/array/intersect_of_props.d.ts.map +1 -1
- package/esm/array/last.d.ts +16 -2
- package/esm/array/last.d.ts.map +1 -1
- package/esm/array/literal_array.d.ts +5 -0
- package/esm/array/literal_array.d.ts.map +1 -1
- package/esm/array/literal_array.js +5 -0
- package/esm/array/literal_array.js.map +1 -1
- package/esm/array/pad_start.d.ts +5 -5
- package/esm/array/pad_start.d.ts.map +1 -1
- package/esm/array/reduce_while.d.ts +6 -0
- package/esm/array/reduce_while.d.ts.map +1 -1
- package/esm/array/reduce_while.js +6 -0
- package/esm/array/reduce_while.js.map +1 -1
- package/esm/array/reverse.d.ts +5 -0
- package/esm/array/reverse.d.ts.map +1 -1
- package/esm/array/union_of_props.d.ts +1 -1
- package/esm/array/union_of_props.d.ts.map +1 -1
- package/esm/functional/compose.d.ts +2 -1
- package/esm/functional/compose.d.ts.map +1 -1
- package/esm/functional/compose.js.map +1 -1
- package/esm/functional/context.d.ts.map +1 -1
- package/esm/index.d.ts +19 -3
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +4 -2
- package/esm/index.js.map +1 -1
- package/esm/math/numeric_struct.d.ts +1 -1
- package/esm/math/numeric_struct.d.ts.map +1 -1
- package/esm/mix_types/any_or_never_type.d.ts +18 -10
- package/esm/mix_types/any_or_never_type.d.ts.map +1 -1
- package/esm/nodejs/isNodeError.d.ts.map +1 -1
- package/esm/nominal/brand.d.ts.map +1 -1
- package/esm/nominal/flavor.d.ts.map +1 -1
- package/esm/testing/test_type.d.ts +44 -1
- package/esm/testing/test_type.d.ts.map +1 -1
- package/esm/tuple/common_prop_keys.d.ts +29 -0
- package/esm/tuple/common_prop_keys.d.ts.map +1 -0
- package/esm/tuple/common_prop_keys.js.map +1 -0
- package/esm/tuple/create_tuple.d.ts.map +1 -1
- package/esm/tuple/drop.d.ts +72 -0
- package/esm/tuple/drop.d.ts.map +1 -0
- package/esm/tuple/drop.js +9 -0
- package/esm/tuple/drop.js.map +1 -0
- package/{cjs/array → esm/tuple}/tail.d.ts +1 -1
- package/esm/{array → tuple}/tail.d.ts.map +1 -1
- package/esm/{array → tuple}/tail.js.map +1 -1
- package/esm/tuple/tuple_plus.d.ts +2 -1
- package/esm/tuple/tuple_plus.d.ts.map +1 -1
- package/esm/tuple/tuple_plus.filter.d.ts +13 -0
- package/esm/tuple/tuple_plus.filter.d.ts.map +1 -0
- package/esm/tuple/tuple_plus.filter.js +2 -0
- package/esm/tuple/tuple_plus.filter.js.map +1 -0
- package/esm/tuple/tuple_plus.js +1 -1
- package/esm/tuple/tuple_plus.js.map +1 -1
- package/esm/tuple/tuple_plus.pad_start.d.ts +8 -3
- package/esm/tuple/tuple_plus.pad_start.d.ts.map +1 -1
- package/package.json +15 -11
- package/ts/any/any_type.ts +8 -9
- package/ts/any/readme.md +91 -7
- package/ts/array/array.at.ts +3 -1
- package/ts/array/array.find.ts +3 -1
- package/ts/array/array.find_last.ts +3 -1
- package/ts/array/array.some.ts +1 -1
- package/ts/array/{array.concat.ts → array_plus.concat.ts} +3 -0
- package/ts/array/array_plus.filter.ts +5 -0
- package/ts/array/array_plus.pad_start.ts +13 -0
- package/ts/array/array_plus.ts +3 -2
- package/ts/array/array_type.ts +30 -12
- package/ts/array/filter.ts +27 -22
- package/ts/array/head.ts +18 -1
- package/ts/array/intersect_of_props.ts +7 -3
- package/ts/array/last.ts +16 -2
- package/ts/array/literal_array.ts +5 -0
- package/ts/array/pad_start.ts +6 -10
- package/ts/array/readme.md +181 -16
- package/ts/array/reduce_while.ts +6 -0
- package/ts/array/reverse.ts +5 -0
- package/ts/array/union_of_props.ts +1 -1
- package/ts/assertion/readme.md +3 -3
- package/ts/functional/compose.ts +2 -1
- package/ts/functional/context.ts +1 -1
- package/ts/index.ts +22 -3
- package/ts/math/numeric_struct.ts +9 -9
- package/ts/mix_types/any_or_never_type.ts +18 -10
- package/ts/mix_types/readme.md +40 -0
- package/ts/nodejs/isNodeError.ts +13 -13
- package/ts/nominal/brand.ts +1 -1
- package/ts/nominal/flavor.ts +1 -1
- package/ts/testing/readme.md +30 -0
- package/ts/testing/test_type.ts +90 -47
- package/ts/tuple/common_prop_keys.ts +55 -0
- package/ts/tuple/create_tuple.ts +9 -9
- package/ts/tuple/drop.ts +124 -0
- package/ts/tuple/readme.md +116 -1
- package/ts/{array → tuple}/tail.ts +1 -1
- package/ts/tuple/tuple_plus.filter.ts +29 -0
- package/ts/tuple/tuple_plus.pad_start.ts +26 -13
- package/ts/tuple/tuple_plus.ts +2 -1
- package/cjs/array/array.concat.d.ts.map +0 -1
- package/cjs/array/array.concat.js.map +0 -1
- package/cjs/array/common_prop_keys.d.ts +0 -11
- package/cjs/array/common_prop_keys.d.ts.map +0 -1
- package/cjs/array/common_prop_keys.js.map +0 -1
- package/cjs/array/drop.d.ts +0 -19
- package/cjs/array/drop.d.ts.map +0 -1
- package/cjs/array/drop.js.map +0 -1
- package/cjs/array/index.d.ts +0 -17
- package/cjs/array/index.d.ts.map +0 -1
- package/cjs/array/index.js +0 -33
- package/cjs/array/index.js.map +0 -1
- package/esm/array/array.concat.d.ts.map +0 -1
- package/esm/array/array.concat.js +0 -2
- package/esm/array/array.concat.js.map +0 -1
- package/esm/array/common_prop_keys.d.ts +0 -11
- package/esm/array/common_prop_keys.d.ts.map +0 -1
- package/esm/array/common_prop_keys.js.map +0 -1
- package/esm/array/drop.d.ts +0 -19
- package/esm/array/drop.d.ts.map +0 -1
- package/esm/array/drop.js +0 -7
- package/esm/array/drop.js.map +0 -1
- package/esm/array/index.d.ts +0 -17
- package/esm/array/index.d.ts.map +0 -1
- package/esm/array/index.js +0 -17
- package/esm/array/index.js.map +0 -1
- package/ts/array/common_prop_keys.ts +0 -18
- package/ts/array/drop.ts +0 -71
- package/ts/array/index.ts +0 -16
- /package/cjs/{array → tuple}/common_prop_keys.js +0 -0
- /package/cjs/{array → tuple}/tail.js +0 -0
- /package/esm/{array → tuple}/common_prop_keys.js +0 -0
- /package/esm/{array → tuple}/tail.js +0 -0
package/ts/any/readme.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Any
|
|
2
2
|
|
|
3
|
-
`any` is one of the two top
|
|
3
|
+
`any` is one of the two top types in TypeScript.
|
|
4
|
+
It is a supertype of all types.
|
|
5
|
+
It is a way to opt-out of type checking and let the values pass through compile-time checks.
|
|
4
6
|
|
|
5
7
|
## Type Checking
|
|
6
8
|
|
|
@@ -10,14 +12,96 @@ They are strict type checks, meaning they match only the type `any`.
|
|
|
10
12
|
Union and intersections are not a factor here they are resolved to `any`,
|
|
11
13
|
except `any & never` which is `never`.
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
- [`IsAny<T, Then = true, Else = false`](any_type.ts#L30): is `T` exactly `any`.
|
|
15
|
-
- [`NotAnyType<T, Then = T, Else = never>`](any_type.ts#L45): check if `T` is not exactly `any`.
|
|
16
|
-
- [`IsNotAny<T, Then = true, Else = false>`](any_type.ts#L60): is `T` not exactly `any`.
|
|
15
|
+
### [AnyType](./any.spec.ts#15)
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
`AnyType<T, Then = T, Else = never>`
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
🌪️ *filter*
|
|
20
|
+
|
|
21
|
+
Filter `T` to ensure it is exactly `any`.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import type { AnyType } from 'type-plus'
|
|
25
|
+
|
|
26
|
+
type R = AnyType<any> // any
|
|
27
|
+
|
|
28
|
+
type R = AnyType<never> // never
|
|
29
|
+
type R = AnyType<unknown> // never
|
|
30
|
+
type R = AnyType<string | boolean> // never
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### [IsAny](./any.spec.ts#31)
|
|
34
|
+
|
|
35
|
+
`IsAny<T, Then = true, Else = false>`
|
|
36
|
+
|
|
37
|
+
🎭 *validate*
|
|
38
|
+
|
|
39
|
+
Validate if `T` is exactly `any`.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import type { IsAny } from 'type-plus'
|
|
43
|
+
|
|
44
|
+
type R = IsAny<any> // true
|
|
45
|
+
|
|
46
|
+
type R = IsAny<never> // false
|
|
47
|
+
type R = IsAny<unknown> // false
|
|
48
|
+
type R = IsAny<string | boolean> // false
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### [NotAnyType](./any.spec.ts#47)
|
|
52
|
+
|
|
53
|
+
`NotAnyType<T, Then = T, Else = never>`
|
|
54
|
+
|
|
55
|
+
🌪️ *filter*
|
|
56
|
+
|
|
57
|
+
Filter `T` to ensure it is not exactly `any`.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import type { NotAnyType } from 'type-plus'
|
|
61
|
+
|
|
62
|
+
type R = NotAnyType<any> // never
|
|
63
|
+
|
|
64
|
+
type R = NotAnyType<never> // never
|
|
65
|
+
type R = NotAnyType<unknown> // never
|
|
66
|
+
type R = NotAnyType<string | boolean> // string | boolean
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### [IsNotAny](./any.spec.ts#65)
|
|
70
|
+
|
|
71
|
+
`IsNotAny<T, Then = true, Else = false>`
|
|
72
|
+
|
|
73
|
+
🎭 *validate*
|
|
74
|
+
|
|
75
|
+
Validate if `T` is not exactly `any`.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import type { IsNotAny } from 'type-plus'
|
|
79
|
+
|
|
80
|
+
type R = IsNotAny<any> // false
|
|
81
|
+
|
|
82
|
+
type R = IsNotAny<never> // true
|
|
83
|
+
type R = IsNotAny<unknown> // true
|
|
84
|
+
type R = IsNotAny<string | boolean> // true
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Trivia
|
|
88
|
+
|
|
89
|
+
> Both `any` and `unknown` are top types?
|
|
90
|
+
|
|
91
|
+
Well, yeah:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
type A = any extends unknown ? 1 : 2 // 1
|
|
95
|
+
type B = unknown extends any ? 1 : 2 // 1
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> Aren't using `any` is bad?
|
|
99
|
+
|
|
100
|
+
If you use it to simply tell TypeScript to shut up because you are lazy, then yes.
|
|
101
|
+
|
|
102
|
+
But it is ok to use `any` in many cases, as the type system of TypeScript is not sounded.
|
|
103
|
+
|
|
104
|
+
There are many cases it is not possible to induce the type correctly.
|
|
21
105
|
|
|
22
106
|
## References
|
|
23
107
|
|
package/ts/array/array.at.ts
CHANGED
|
@@ -3,7 +3,9 @@ import type { TupleType } from '../tuple/tuple_type.js'
|
|
|
3
3
|
import type { IndexAt } from './array_index.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* 🦴 *utilities*
|
|
7
|
+
*
|
|
8
|
+
* Gets the type of the array or tuple at positive or negative index `N`.
|
|
7
9
|
*
|
|
8
10
|
* Like `Array.at()`, this type supports negative numbers.
|
|
9
11
|
*
|
package/ts/array/array.find.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { TupleType } from '../tuple/tuple_type.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 🦴 *utilities*
|
|
5
|
+
*
|
|
6
|
+
* Gets the first type in the array or tuple that matches the `Criteria`.
|
|
5
7
|
*
|
|
6
8
|
* If the `Criteria` is not met, it will return `never'.
|
|
7
9
|
*
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { TupleType } from '../tuple/tuple_type.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 🦴 *utilities*
|
|
5
|
+
*
|
|
6
|
+
* Gets the last type in the array or tuple that matches the `Criteria`.
|
|
5
7
|
*
|
|
6
8
|
* If the `Criteria` is not met, it will return `never'.
|
|
7
9
|
*
|
package/ts/array/array.some.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CanAssign } from '../index.js'
|
|
2
|
+
import type { CreateTuple } from '../tuple/create_tuple.js'
|
|
3
|
+
import type { UnionOfValues } from './union_of_values.js'
|
|
4
|
+
|
|
5
|
+
export type PadStart<
|
|
6
|
+
A extends unknown[],
|
|
7
|
+
MaxLength extends number,
|
|
8
|
+
PadWith = unknown
|
|
9
|
+
> = MaxLength extends 0
|
|
10
|
+
? A
|
|
11
|
+
: CanAssign<PadWith, UnionOfValues<A>> extends true
|
|
12
|
+
? A
|
|
13
|
+
: PadStart<[...CreateTuple<MaxLength, PadWith>, ...A], MaxLength, PadWith>
|
package/ts/array/array_plus.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type { At } from './array.at.js'
|
|
2
|
-
export type { Concat } from './
|
|
2
|
+
export type { Concat } from './array_plus.concat.js'
|
|
3
3
|
export type { Entries } from './array.entries.js'
|
|
4
4
|
export type { FindFirst as Find } from './array.find.js'
|
|
5
5
|
export type { FindLast } from './array.find_last.js'
|
|
6
6
|
export type { Reverse } from './array.reverse.js'
|
|
7
7
|
export type { Some } from './array.some.js'
|
|
8
8
|
export type { IndexAt, IsIndexOutOfBound } from './array_index.js'
|
|
9
|
+
export type { Filter } from './array_plus.filter.js'
|
|
9
10
|
export type { SplitAt } from './array_plus.split_at.js'
|
|
10
|
-
export type { PadStart } from './pad_start.js'
|
|
11
|
+
export type { PadStart } from './array_plus.pad_start.js'
|
package/ts/array/array_type.ts
CHANGED
|
@@ -2,12 +2,17 @@ import type { IsNever } from '../never/never_type.js'
|
|
|
2
2
|
import type { StrictNumberType } from '../number/strict_number_type.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 🌪️ *filter*
|
|
6
6
|
*
|
|
7
|
+
* Filter `T` to ensure it is an array, excluding tuple.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
7
10
|
* ```ts
|
|
8
11
|
* type R = ArrayType<number[]> // number[]
|
|
9
12
|
*
|
|
10
13
|
* type R = ArrayType<[1]> // never
|
|
14
|
+
* type R = ArrayType<number[] | 1> // never
|
|
15
|
+
* type R = ArrayType<number[] & { a: 1 }> // never
|
|
11
16
|
* ```
|
|
12
17
|
*/
|
|
13
18
|
export type ArrayType<T, Then = T, Else = never> = IsNever<
|
|
@@ -17,32 +22,45 @@ export type ArrayType<T, Then = T, Else = never> = IsNever<
|
|
|
17
22
|
>
|
|
18
23
|
|
|
19
24
|
/**
|
|
20
|
-
*
|
|
25
|
+
* 🎭 *validate*
|
|
26
|
+
*
|
|
27
|
+
* Validate that `T` is an array, excluding tuple.
|
|
21
28
|
*
|
|
29
|
+
* @example
|
|
22
30
|
* ```ts
|
|
23
|
-
* type R =
|
|
31
|
+
* type R = IsArray<number[]> // true
|
|
24
32
|
*
|
|
25
|
-
* type R =
|
|
26
|
-
* type R =
|
|
33
|
+
* type R = IsArray<number> // false
|
|
34
|
+
* type R = IsArray<[1]> // false
|
|
27
35
|
* ```
|
|
28
36
|
*/
|
|
29
|
-
export type
|
|
37
|
+
export type IsArray<T, Then = true, Else = false> = ArrayType<T, Then, Else>
|
|
30
38
|
|
|
31
39
|
/**
|
|
32
|
-
*
|
|
40
|
+
* 🌪️ *filter*
|
|
41
|
+
*
|
|
42
|
+
* Filter `T` to ensure it is not an array, excluding tuple.
|
|
33
43
|
*
|
|
44
|
+
* i.e. *tuple* will pass through this filter.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
34
47
|
* ```ts
|
|
35
|
-
* type R =
|
|
48
|
+
* type R = NotArrayType<number[]> // never
|
|
36
49
|
*
|
|
37
|
-
* type R =
|
|
38
|
-
* type R =
|
|
50
|
+
* type R = NotArrayType<number> // number
|
|
51
|
+
* type R = NotArrayType<[1]> // [1]
|
|
39
52
|
* ```
|
|
40
53
|
*/
|
|
41
|
-
export type
|
|
54
|
+
export type NotArrayType<T, Then = T, Else = never> = ArrayType<T, Else, Then>
|
|
42
55
|
|
|
43
56
|
/**
|
|
44
|
-
*
|
|
57
|
+
* 🎭 *validate*
|
|
58
|
+
*
|
|
59
|
+
* Validate that `T` is not an array, excluding tuple.
|
|
60
|
+
*
|
|
61
|
+
* i.e. *tuple* will pass through this filter.
|
|
45
62
|
*
|
|
63
|
+
* @example
|
|
46
64
|
* ```ts
|
|
47
65
|
* type R = IsNotArray<number[]> // false
|
|
48
66
|
*
|
package/ts/array/filter.ts
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
+
import type { Filter as FilterTuple } from '../tuple/tuple_plus.filter.js'
|
|
2
|
+
import type { Filter as FilterArray } from './array_plus.filter.js'
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* ⚗️ *transform*
|
|
6
|
+
*
|
|
7
|
+
* Filter the array or tuple `A`, keeping entries satisfying `Criteria`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* type R = Filter<[1, 2, '3'], number> // [1, 2]
|
|
12
|
+
* type R = Filter<Array<string | undefined>, string> // string[]
|
|
13
|
+
* ```
|
|
3
14
|
*/
|
|
4
|
-
export type Filter<A extends
|
|
5
|
-
?
|
|
6
|
-
|
|
7
|
-
? A
|
|
8
|
-
: Criteria extends A[0]
|
|
9
|
-
? Array<Criteria>
|
|
10
|
-
: never[]
|
|
11
|
-
: // tuple
|
|
12
|
-
A['length'] extends 0
|
|
13
|
-
? never
|
|
14
|
-
: A extends [infer Head, ...infer Tail]
|
|
15
|
-
? Tail['length'] extends 0
|
|
16
|
-
? Head extends Criteria
|
|
17
|
-
? [Head]
|
|
18
|
-
: never[]
|
|
19
|
-
: Head extends Criteria
|
|
20
|
-
? [Head, ...Filter<Tail, Criteria>]
|
|
21
|
-
: Filter<Tail, Criteria>
|
|
22
|
-
: never
|
|
15
|
+
export type Filter<A extends unknown[], Criteria> = number extends A['length']
|
|
16
|
+
? FilterArray<A, Criteria>
|
|
17
|
+
: FilterTuple<A, Criteria>
|
|
23
18
|
|
|
24
19
|
/**
|
|
25
|
-
*
|
|
20
|
+
* ⚗️ *transform*
|
|
21
|
+
* 👽 *alias*
|
|
22
|
+
*
|
|
23
|
+
* Keeps entries satisfying `Criteria` in array or tuple `A`.
|
|
24
|
+
*
|
|
25
|
+
* @alias of [Filter](./filter.ts)
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* type R = KeepMatch<[1, 2, 3], number> // [1, 2]
|
|
29
|
+
* type R = KeepMatch<Array<string|undefined>, string> // string[]
|
|
30
|
+
* ```
|
|
26
31
|
*/
|
|
27
|
-
export type KeepMatch<A extends
|
|
32
|
+
export type KeepMatch<A extends unknown[], Criteria> = Filter<A, Criteria>
|
package/ts/array/head.ts
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 🦴 *utilities*
|
|
3
|
+
*
|
|
4
|
+
* Gets the first entry in the tuple or the type of array.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type R = Head<[1, 2, 3]> // 1
|
|
9
|
+
* type R = Head<string[]> // string
|
|
10
|
+
*
|
|
11
|
+
* type R = Head<[]> // never
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export type Head<T extends any[], Cases extends {
|
|
15
|
+
empty_tuple?: unknown
|
|
16
|
+
} = {
|
|
17
|
+
empty_tuple: never
|
|
18
|
+
}> = T['length'] extends 0 ? Cases['empty_tuple'] : T[0]
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { KeyTypes } from '../object/index.js'
|
|
2
|
-
import type { Tail } from '
|
|
2
|
+
import type { Tail } from '../tuple/tail.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 🦴 *utilities*
|
|
6
|
+
*
|
|
7
|
+
* Gets the intersect of properties of the elements in `A`.
|
|
6
8
|
*/
|
|
7
|
-
export type IntersectOfProps<A extends Record<any, unknown>[], P extends KeyTypes> = A['length']
|
|
9
|
+
export type IntersectOfProps<A extends Record<any, unknown>[], P extends KeyTypes> = number extends A['length']
|
|
10
|
+
? A[0][P]
|
|
11
|
+
: A['length'] extends 0
|
|
8
12
|
? never
|
|
9
13
|
: A['length'] extends 1
|
|
10
14
|
? A[0][P]
|
package/ts/array/last.ts
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 🦴 *utilities*
|
|
3
|
+
*
|
|
4
|
+
* Gets the last entry in the tuple or the type of array.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type R = Last<[1, 2, 3]> // 3
|
|
9
|
+
* type R = Last<string[]> // string
|
|
10
|
+
*
|
|
11
|
+
* type R = Last<[]> // never
|
|
12
|
+
* ```
|
|
3
13
|
*/
|
|
4
|
-
export type Last<T extends
|
|
14
|
+
export type Last<T extends unknown[], Cases extends {
|
|
15
|
+
empty_tuple?: unknown
|
|
16
|
+
} = {
|
|
17
|
+
empty_tuple: never
|
|
18
|
+
}> = T['length'] extends 0 ? Cases['empty_tuple'] : T extends [...unknown[], infer R] ? R : T[0]
|
package/ts/array/pad_start.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { PadStart as TuplePadStart } from '../tuple/tuple_plus.pad_start.js'
|
|
4
|
-
import type { UnionOfValues } from './union_of_values.js'
|
|
1
|
+
import type { PadStart as PadStartTuple } from '../tuple/tuple_plus.pad_start.js'
|
|
2
|
+
import type { PadStart as PadStartArray } from './array_plus.pad_start.js'
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* Pads the start of an array or tuple with `PadWith`.
|
|
8
6
|
*
|
|
7
|
+
* ⚗️ *transform*
|
|
8
|
+
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
11
11
|
* // Padding array
|
|
@@ -30,12 +30,8 @@ export type PadStart<
|
|
|
30
30
|
MaxLength extends number,
|
|
31
31
|
PadWith = unknown
|
|
32
32
|
> = number extends A['length']
|
|
33
|
-
? MaxLength
|
|
34
|
-
|
|
35
|
-
: CanAssign<PadWith, UnionOfValues<A>> extends true
|
|
36
|
-
? A
|
|
37
|
-
: PadStart<[...CreateTuple<MaxLength, PadWith>, ...A], MaxLength, PadWith>
|
|
38
|
-
: TuplePadStart<A, MaxLength, PadWith>
|
|
33
|
+
? PadStartArray<A, MaxLength, PadWith>
|
|
34
|
+
: PadStartTuple<A, MaxLength, PadWith>
|
|
39
35
|
|
|
40
36
|
/**
|
|
41
37
|
* @deprecated use PadStart instead
|
package/ts/array/readme.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# Array
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
In TypeScript, the type of an JavaScript array can be an [array](#array) or a [tuple](../tuple/readme.md).
|
|
4
|
+
|
|
5
|
+
`Array<T>` or `T[]` is a type that represents an array of `T`
|
|
6
|
+
|
|
7
|
+
There is no length limitation to an `Array`,
|
|
8
|
+
and each element has the same type `T`.
|
|
4
9
|
|
|
5
10
|
## Type Checking
|
|
6
11
|
|
|
@@ -9,6 +14,14 @@ The `ArrayType<T>` and friends are used to check if a type is exactly `Array<T>`
|
|
|
9
14
|
They are strict type checks, meaning they match only the type `Array<T>`,
|
|
10
15
|
and not [tuple], union, or intersection types.
|
|
11
16
|
|
|
17
|
+
### [ArrayType](./array_type.ts#18)
|
|
18
|
+
|
|
19
|
+
`ArrayType<T, Then = T, Else = never>`
|
|
20
|
+
|
|
21
|
+
🌪️ *filter*
|
|
22
|
+
|
|
23
|
+
Filter `T` to ensure it is an array, excluding tuple.
|
|
24
|
+
|
|
12
25
|
```ts
|
|
13
26
|
import type { ArrayType } from 'type-plus'
|
|
14
27
|
|
|
@@ -19,25 +32,64 @@ type R = ArrayType<number[] | 1> // never
|
|
|
19
32
|
type R = ArrayType<number[] & { a: 1 }> // never
|
|
20
33
|
```
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
- [`IsArray<T, Then = true, Else = false`](array_type.ts#L33): is `T` an array and not a tuple.
|
|
24
|
-
- [`NotArrayType<T, Then = T, Else = never>`](array_type.ts#L47): check if `T` is not an array (can be a tuple).
|
|
25
|
-
- [`IsNotArray<T, Then = true, Else = false>`](array_type.ts#L61): is `T` not an array (can be a tuple).
|
|
35
|
+
### [IsArray](./array_type.ts#37)
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
`IsArray<T, Then = true, Else = false>`
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
Whenever possible, these types and type utilities also work with *tuples*,
|
|
31
|
-
as *tuples* is a subset of array.
|
|
39
|
+
🎭 *validate*
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
please check [`TuplePlus`](../tuple/readme.md#TuplePlus).
|
|
41
|
+
Validate that `T` is an array, excluding tuple.
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
```ts
|
|
44
|
+
import type { IsArray } from 'type-plus'
|
|
45
|
+
|
|
46
|
+
type R = IsArray<number[]> // true
|
|
47
|
+
|
|
48
|
+
type R = IsArray<number> // false
|
|
49
|
+
type R = IsArray<[1]> // false
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### [NotArrayType](./array_type.ts#54)
|
|
53
|
+
|
|
54
|
+
`NotArrayType<T, Then = T, Else = never>`
|
|
55
|
+
|
|
56
|
+
🌪️ *filter*
|
|
57
|
+
|
|
58
|
+
Filter `T` to ensure it is not an array, excluding tuple.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import type { NotArrayType } from 'type-plus'
|
|
62
|
+
|
|
63
|
+
type R = NotArrayType<number[]> // never
|
|
64
|
+
|
|
65
|
+
type R = NotArrayType<number> // number
|
|
66
|
+
type R = NotArrayType<[1]> // [1]
|
|
67
|
+
```
|
|
37
68
|
|
|
38
|
-
|
|
69
|
+
### [IsNotArrayType](./array_type.ts#71)
|
|
39
70
|
|
|
40
|
-
|
|
71
|
+
`IsNotArrayType<T, Then = true, Else = false>`
|
|
72
|
+
|
|
73
|
+
🎭 *validate*
|
|
74
|
+
|
|
75
|
+
Validate that `T` is not an array, excluding tuple.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import type { IsNotArrayType } from 'type-plus'
|
|
79
|
+
|
|
80
|
+
type R = IsNotArrayType<number[]> // false
|
|
81
|
+
|
|
82
|
+
type R = IsNotArrayType<number> // true
|
|
83
|
+
type R = IsNotArrayType<[1]> // true
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## [At](./array.at.ts)
|
|
87
|
+
|
|
88
|
+
`At<A, N, Fail = never>`
|
|
89
|
+
|
|
90
|
+
🦴 *utilities*
|
|
91
|
+
|
|
92
|
+
Gets the type of the array or tuple at positive or negative index `N`.
|
|
41
93
|
|
|
42
94
|
For array, it will return the union of the type of the array value and `undefined`,
|
|
43
95
|
as there is no way to guarantee the array has value at `N`.
|
|
@@ -61,9 +113,12 @@ If the `N` is out of bound,
|
|
|
61
113
|
or `N` is not a valid index,
|
|
62
114
|
`ArrayPlus.At` will return the `Fail` case, which defaults to `never`.
|
|
63
115
|
|
|
64
|
-
|
|
116
|
+
## [`Concat`](./array.concat.ts#L12)
|
|
117
|
+
|
|
118
|
+
`Concat<A, B>`
|
|
65
119
|
|
|
66
|
-
|
|
120
|
+
🦴 *utilities*
|
|
121
|
+
💀 *deprecated* Will be available only as `ArrayPlus.Concat` in the next version
|
|
67
122
|
|
|
68
123
|
Concats two arrays or tuples.
|
|
69
124
|
|
|
@@ -72,6 +127,116 @@ It is added for completeness.
|
|
|
72
127
|
|
|
73
128
|
You are encouraged to use `[...A, ...B]` directly.
|
|
74
129
|
|
|
130
|
+
## [`FindFirst`](./array.find.ts)
|
|
131
|
+
|
|
132
|
+
## [`FineLast`](./array.find_last.ts)
|
|
133
|
+
|
|
134
|
+
## [`Some`](./array.some.ts)
|
|
135
|
+
|
|
136
|
+
## [`Filter`](./filter.ts#l17)
|
|
137
|
+
|
|
138
|
+
`Filter<A, Criteria>`
|
|
139
|
+
|
|
140
|
+
⚗️ *transform*
|
|
141
|
+
|
|
142
|
+
Filter the array or tuple `A`, keeping entries satisfying `Criteria`.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import type { Filter } from 'type-plus'
|
|
146
|
+
|
|
147
|
+
type R = Filter<[1, 2, '3'], number> // [1, 2]
|
|
148
|
+
type R = Filter<Array<string | undefined>, string> // string[]
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## [`KeepMatch`](./filter.ts)
|
|
152
|
+
|
|
153
|
+
`KeepMatch<A, Criteria>`
|
|
154
|
+
|
|
155
|
+
⚗️ *transform*
|
|
156
|
+
👽 *alias* of [`Filter`](#filter)
|
|
157
|
+
|
|
158
|
+
Keeps entries satisfying `Criteria` in array or tuple `A`.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import type { KeepMatch } from 'type-plus'
|
|
162
|
+
|
|
163
|
+
type R = KeepMatch<[1, 2, '3'], number> // [1, 2]
|
|
164
|
+
type R = KeepMatch<Array<string | undefined>, string> // string[]
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## [`Head`](./head.ts#l14)
|
|
168
|
+
|
|
169
|
+
`Head<T, Cases = { empty_tuple }>`
|
|
170
|
+
|
|
171
|
+
🦴 *utilities*
|
|
172
|
+
|
|
173
|
+
Gets the first entry in the tuple or the type of array.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import type { Head } from 'type-plus'
|
|
177
|
+
|
|
178
|
+
type R = Head<[1, 2, 3]> // 1
|
|
179
|
+
type R = Head<string[]> // string
|
|
180
|
+
|
|
181
|
+
type R = Head<[]> // never
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## [`IntersectOfProps`](./intersect_of_props.ts)
|
|
185
|
+
|
|
186
|
+
## [`MapToProp`](./intersect_of_props.ts)
|
|
187
|
+
|
|
188
|
+
## [`Last`](./last.ts)
|
|
189
|
+
|
|
190
|
+
`Last<T, Cases = { empty_tuple }>`
|
|
191
|
+
|
|
192
|
+
🦴 *utilities*
|
|
193
|
+
|
|
194
|
+
Gets the last entry in the tuple or the type of array.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
import type { Last } from 'type-plus'
|
|
198
|
+
|
|
199
|
+
type R = Last<[1, 2, 3]> // 3
|
|
200
|
+
type R = Last<string[]> // string
|
|
201
|
+
|
|
202
|
+
type R = Last<[]> // never
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## [`literalArray`](./literal_array.ts)
|
|
206
|
+
|
|
207
|
+
## [`PadStart`](./pad_start.ts)
|
|
208
|
+
|
|
209
|
+
## [`reduceWhile`](./reduce_while.ts)
|
|
210
|
+
|
|
211
|
+
## [`Reverse`](./reverse.ts)
|
|
212
|
+
|
|
213
|
+
## [`PropUnion`](./union_of_props.ts)
|
|
214
|
+
|
|
215
|
+
## [`UnionOfProps`](./union_of_props.ts)
|
|
216
|
+
|
|
217
|
+
## [`UnionOfValues`](./union_of_values.ts)
|
|
218
|
+
|
|
219
|
+
## ArrayPlus
|
|
220
|
+
|
|
221
|
+
`ArrayPlus` contains all types and type utilities related to array.
|
|
222
|
+
Whenever possible, these types and type utilities also work with *tuples*,
|
|
223
|
+
as *tuples* is a subset of array.
|
|
224
|
+
|
|
225
|
+
For *tuple* specific types and type utilities,
|
|
226
|
+
please check [`TuplePlus`](../tuple/readme.md#TuplePlus).
|
|
227
|
+
|
|
228
|
+
### [`ArrayPlus.At`](./array.at.ts#L18)
|
|
229
|
+
|
|
230
|
+
`ArrayPlus.At<A, N, Fail = never>`
|
|
231
|
+
|
|
232
|
+
Alias of [At](#at).
|
|
233
|
+
|
|
234
|
+
### [`ArrayPlus.Concat`](./array.concat.ts#L12)
|
|
235
|
+
|
|
236
|
+
`ArrayPlus.Concat<A, B>`
|
|
237
|
+
|
|
238
|
+
Alias of [Concat](#concat).
|
|
239
|
+
|
|
75
240
|
### [`ArrayPlus.Entries`](./array.entries.ts#L14)
|
|
76
241
|
|
|
77
242
|
> `ArrayPlus.Entries<A>`
|
package/ts/array/reduce_while.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🦴 *utilities*
|
|
3
|
+
*
|
|
4
|
+
* `reduce()` with predicate for early termination.
|
|
5
|
+
* A simple version of the same function in the `ramda` package.
|
|
6
|
+
*/
|
|
1
7
|
export function reduceWhile<T, R>(
|
|
2
8
|
predicate: (acc: R, currentValue: T) => boolean,
|
|
3
9
|
callbackfn: (previousValue: R, currentValue: T, currentIndex: number, array: T[]) => R,
|