type-fest 4.11.1 → 4.13.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/index.d.ts +10 -0
- package/package.json +1 -1
- package/readme.md +11 -0
- package/source/array-slice.d.ts +106 -0
- package/source/array-splice.d.ts +4 -2
- package/source/distributed-omit.d.ts +89 -0
- package/source/greater-than-or-equal.d.ts +22 -0
- package/source/greater-than.d.ts +49 -0
- package/source/int-range.d.ts +2 -1
- package/source/internal.d.ts +290 -47
- package/source/less-than-or-equal.d.ts +22 -0
- package/source/less-than.d.ts +22 -0
- package/source/multidimensional-array.d.ts +1 -1
- package/source/multidimensional-readonly-array.d.ts +1 -1
- package/source/numeric.d.ts +17 -0
- package/source/omit-deep.d.ts +9 -5
- package/source/set-parameter-type.d.ts +60 -14
- package/source/string-slice.d.ts +37 -0
- package/source/subtract.d.ts +68 -0
- package/source/sum.d.ts +68 -0
package/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './source/observable-like';
|
|
|
6
6
|
|
|
7
7
|
// Utilities
|
|
8
8
|
export type {KeysOfUnion} from './source/keys-of-union';
|
|
9
|
+
export type {DistributedOmit} from './source/distributed-omit';
|
|
9
10
|
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
|
|
10
11
|
export type {NonEmptyObject} from './source/non-empty-object';
|
|
11
12
|
export type {UnknownRecord} from './source/unknown-record';
|
|
@@ -46,6 +47,7 @@ export type {ConditionalPick} from './source/conditional-pick';
|
|
|
46
47
|
export type {ConditionalPickDeep, ConditionalPickDeepOptions} from './source/conditional-pick-deep';
|
|
47
48
|
export type {UnionToIntersection} from './source/union-to-intersection';
|
|
48
49
|
export type {Stringified} from './source/stringified';
|
|
50
|
+
export type {StringSlice} from './source/string-slice';
|
|
49
51
|
export type {FixedLengthArray} from './source/fixed-length-array';
|
|
50
52
|
export type {MultidimensionalArray} from './source/multidimensional-array';
|
|
51
53
|
export type {MultidimensionalReadonlyArray} from './source/multidimensional-readonly-array';
|
|
@@ -72,7 +74,14 @@ export type {
|
|
|
72
74
|
NonNegative,
|
|
73
75
|
NegativeInteger,
|
|
74
76
|
NonNegativeInteger,
|
|
77
|
+
IsNegative,
|
|
75
78
|
} from './source/numeric';
|
|
79
|
+
export type {GreaterThan} from './source/greater-than';
|
|
80
|
+
export type {GreaterThanOrEqual} from './source/greater-than-or-equal';
|
|
81
|
+
export type {LessThan} from './source/less-than';
|
|
82
|
+
export type {LessThanOrEqual} from './source/less-than-or-equal';
|
|
83
|
+
export type {Sum} from './source/sum';
|
|
84
|
+
export type {Subtract} from './source/subtract';
|
|
76
85
|
export type {StringKeyOf} from './source/string-key-of';
|
|
77
86
|
export type {Exact} from './source/exact';
|
|
78
87
|
export type {ReadonlyTuple} from './source/readonly-tuple';
|
|
@@ -104,6 +113,7 @@ export type {IsUnknown} from './source/is-unknown';
|
|
|
104
113
|
export type {IfUnknown} from './source/if-unknown';
|
|
105
114
|
export type {ArrayIndices} from './source/array-indices';
|
|
106
115
|
export type {ArrayValues} from './source/array-values';
|
|
116
|
+
export type {ArraySlice} from './source/array-slice';
|
|
107
117
|
export type {ArraySplice} from './source/array-splice';
|
|
108
118
|
export type {SetFieldType} from './source/set-field-type';
|
|
109
119
|
export type {Paths} from './source/paths';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -182,6 +182,7 @@ Click the type names for complete docs.
|
|
|
182
182
|
- [`SetFieldType`](source/set-field-type.d.ts) - Create a type that changes the type of the given keys.
|
|
183
183
|
- [`Paths`](source/paths.d.ts) - Generate a union of all possible paths to properties in the given object.
|
|
184
184
|
- [`SharedUnionFieldsDeep`](source/shared-union-fields-deep.d.ts) - Create a type with shared fields from a union of object types, deeply traversing nested structures.
|
|
185
|
+
- [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
|
|
185
186
|
|
|
186
187
|
### Type Guard
|
|
187
188
|
|
|
@@ -239,11 +240,13 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
239
240
|
- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
|
|
240
241
|
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
|
|
241
242
|
- [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement.
|
|
243
|
+
- [`StringSlice`](source/string-slice.d.ts) - Returns a string slice of a given range, just like `String#slice()`.
|
|
242
244
|
|
|
243
245
|
### Array
|
|
244
246
|
|
|
245
247
|
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
|
|
246
248
|
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
|
|
249
|
+
- [`ArraySlice`](source/array-slice.d.ts) - Returns an array slice of a given range, just like `Array#slice()`.
|
|
247
250
|
- [`LastArrayElement`](source/last-array-element.d.ts) - Extracts the type of the last element of an array.
|
|
248
251
|
- [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
|
|
249
252
|
- [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
|
|
@@ -263,6 +266,13 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
263
266
|
- [`NonNegative`](source/numeric.d.ts) - A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
|
264
267
|
- [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
265
268
|
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
269
|
+
- [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number.
|
|
270
|
+
- [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number.
|
|
271
|
+
- [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number.
|
|
272
|
+
- [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number.
|
|
273
|
+
- [`LessThanOrEqual`](source/less-than-or-equal.d.ts) - Returns a boolean for whether a given number is less than or equal to another number.
|
|
274
|
+
- [`Sum`](source/sum.d.ts) - Returns the sum of two numbers.
|
|
275
|
+
- [`Subtract`](source/subtract.d.ts) - Returns the difference between two numbers.
|
|
266
276
|
|
|
267
277
|
### Change case
|
|
268
278
|
|
|
@@ -314,6 +324,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
314
324
|
- `RequireOnlyOne` - See [`RequireExactlyOne`](source/require-exactly-one.d.ts)
|
|
315
325
|
- `AtMostOne` - See [`RequireOneOrNone`](source/require-one-or-none.d.ts)
|
|
316
326
|
- `AllKeys` - See [`KeysOfUnion`](source/keys-of-union.d.ts)
|
|
327
|
+
- `Branded` - See [`Opaque`](source/opaque.d.ts)
|
|
317
328
|
|
|
318
329
|
## Tips
|
|
319
330
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type {Sum} from './sum';
|
|
2
|
+
import type {LessThanOrEqual} from './less-than-or-equal';
|
|
3
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal';
|
|
4
|
+
import type {GreaterThan} from './greater-than';
|
|
5
|
+
import type {IsNegative} from './numeric';
|
|
6
|
+
import type {And, Not, ArrayMin} from './internal';
|
|
7
|
+
import type {IsEqual} from './is-equal';
|
|
8
|
+
import type {ArraySplice} from './array-splice';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
Returns an array slice of a given range, just like `Array#slice()`.
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {ArraySlice} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
type T0 = ArraySlice<[0, 1, 2, 3, 4]>;
|
|
18
|
+
//=> [0, 1, 2, 3, 4]
|
|
19
|
+
|
|
20
|
+
type T1 = ArraySlice<[0, 1, 2, 3, 4], 0, -1>;
|
|
21
|
+
//=> [0, 1, 2, 3]
|
|
22
|
+
|
|
23
|
+
type T2 = ArraySlice<[0, 1, 2, 3, 4], 1, -2>;
|
|
24
|
+
//=> [1, 2]
|
|
25
|
+
|
|
26
|
+
type T3 = ArraySlice<[0, 1, 2, 3, 4], -2, 4>;
|
|
27
|
+
//=> [3]
|
|
28
|
+
|
|
29
|
+
type T4 = ArraySlice<[0, 1, 2, 3, 4], -2, -1>;
|
|
30
|
+
//=> [3]
|
|
31
|
+
|
|
32
|
+
type T5 = ArraySlice<[0, 1, 2, 3, 4], 0, -999>;
|
|
33
|
+
//=> []
|
|
34
|
+
|
|
35
|
+
function arraySlice<
|
|
36
|
+
const Array_ extends readonly unknown[],
|
|
37
|
+
Start extends number = 0,
|
|
38
|
+
End extends number = Array_['length'],
|
|
39
|
+
>(array: Array_, start?: Start, end?: End) {
|
|
40
|
+
return array.slice(start, end) as ArraySlice<Array_, Start, End>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const slice = arraySlice([1, '2', {a: 3}, [4, 5]], 0, -1);
|
|
44
|
+
|
|
45
|
+
typeof slice;
|
|
46
|
+
//=> [1, '2', { readonly a: 3; }]
|
|
47
|
+
|
|
48
|
+
slice[2].a;
|
|
49
|
+
//=> 3
|
|
50
|
+
|
|
51
|
+
// @ts-expect-error -- TS2493: Tuple type '[1, "2", {readonly a: 3}]' of length '3' has no element at index '3'.
|
|
52
|
+
slice[3];
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
@category Array
|
|
56
|
+
*/
|
|
57
|
+
export type ArraySlice<
|
|
58
|
+
Array_ extends readonly unknown[],
|
|
59
|
+
Start extends number = never,
|
|
60
|
+
End extends number = never,
|
|
61
|
+
> = And<IsEqual<Start, never>, IsEqual<End, never>> extends true
|
|
62
|
+
? Array_
|
|
63
|
+
: number extends Array_['length']
|
|
64
|
+
? VariableLengthArraySliceHelper<Array_, Start, End>
|
|
65
|
+
: ArraySliceHelper<Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End>;
|
|
66
|
+
|
|
67
|
+
type VariableLengthArraySliceHelper<
|
|
68
|
+
Array_ extends readonly unknown[],
|
|
69
|
+
Start extends number,
|
|
70
|
+
End extends number,
|
|
71
|
+
> = And<Not<IsNegative<Start>>, IsEqual<End, never>> extends true
|
|
72
|
+
? ArraySplice<Array_, 0, Start>
|
|
73
|
+
: And<
|
|
74
|
+
And<Not<IsNegative<Start>>, Not<IsNegative<End>>>,
|
|
75
|
+
IsEqual<GreaterThan<End, Start>, true>
|
|
76
|
+
> extends true
|
|
77
|
+
? ArraySliceByPositiveIndex<Array_, Start, End>
|
|
78
|
+
: [];
|
|
79
|
+
|
|
80
|
+
type ArraySliceHelper<
|
|
81
|
+
Array_ extends readonly unknown[],
|
|
82
|
+
Start extends number = 0,
|
|
83
|
+
End extends number = Array_['length'],
|
|
84
|
+
TraversedElement extends Array<Array_[number]> = [],
|
|
85
|
+
Result extends Array<Array_[number]> = [],
|
|
86
|
+
ArrayLength extends number = Array_['length'],
|
|
87
|
+
PositiveS extends number = IsNegative<Start> extends true
|
|
88
|
+
? Sum<ArrayLength, Start> extends infer AddResult extends number
|
|
89
|
+
? number extends AddResult // (ArrayLength + Start) < 0
|
|
90
|
+
? 0
|
|
91
|
+
: AddResult
|
|
92
|
+
: never
|
|
93
|
+
: Start,
|
|
94
|
+
PositiveE extends number = IsNegative<End> extends true ? Sum<ArrayLength, End> : End,
|
|
95
|
+
> = true extends [IsNegative<PositiveS>, LessThanOrEqual<PositiveE, PositiveS>, GreaterThanOrEqual<PositiveS, ArrayLength>][number]
|
|
96
|
+
? []
|
|
97
|
+
: ArraySliceByPositiveIndex<Array_, ArrayMin<[PositiveS, ArrayLength]>, ArrayMin<[PositiveE, ArrayLength]>>;
|
|
98
|
+
|
|
99
|
+
type ArraySliceByPositiveIndex<
|
|
100
|
+
Array_ extends readonly unknown[],
|
|
101
|
+
Start extends number,
|
|
102
|
+
End extends number,
|
|
103
|
+
Result extends Array<Array_[number]> = [],
|
|
104
|
+
> = Start extends End
|
|
105
|
+
? Result
|
|
106
|
+
: ArraySliceByPositiveIndex<Array_, Sum<Start, 1>, End, [...Result, Array_[Start]]>;
|
package/source/array-splice.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type {BuildTuple,
|
|
1
|
+
import type {BuildTuple, StaticPartOfArray, VariablePartOfArray} from './internal';
|
|
2
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal';
|
|
3
|
+
import type {Subtract} from './subtract';
|
|
2
4
|
import type {UnknownArray} from './unknown-array';
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -23,7 +25,7 @@ type SplitVariableArrayByIndex<T extends UnknownArray,
|
|
|
23
25
|
> =
|
|
24
26
|
SplitIndex extends 0
|
|
25
27
|
? [[], T]
|
|
26
|
-
:
|
|
28
|
+
: GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true
|
|
27
29
|
? [
|
|
28
30
|
SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0],
|
|
29
31
|
[
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type {KeysOfUnion} from './keys-of-union';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Omits keys from a type, distributing the operation over a union.
|
|
5
|
+
|
|
6
|
+
TypeScript's `Omit` doesn't distribute over unions, leading to the erasure of unique properties from union members when omitting keys. This creates a type that only retains properties common to all union members, making it impossible to access member-specific properties after the Omit. Essentially, using `Omit` on a union type merges the types into a less specific one, hindering type narrowing and property access based on discriminants. This type solves that.
|
|
7
|
+
|
|
8
|
+
Example:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
type A = {
|
|
12
|
+
discriminant: 'A';
|
|
13
|
+
foo: string;
|
|
14
|
+
a: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type B = {
|
|
18
|
+
discriminant: 'B';
|
|
19
|
+
foo: string;
|
|
20
|
+
b: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Union = A | B;
|
|
24
|
+
|
|
25
|
+
type OmittedUnion = Omit<Union, 'foo'>;
|
|
26
|
+
//=> {discriminant: 'A' | 'B'}
|
|
27
|
+
|
|
28
|
+
const omittedUnion: OmittedUnion = createOmittedUnion();
|
|
29
|
+
|
|
30
|
+
if (omittedUnion.discriminant === 'A') {
|
|
31
|
+
// We would like to narrow `omittedUnion`'s type
|
|
32
|
+
// to `A` here, but we can't because `Omit`
|
|
33
|
+
// doesn't distribute over unions.
|
|
34
|
+
|
|
35
|
+
omittedUnion.a;
|
|
36
|
+
//=> Error: `a` is not a property of `{discriminant: 'A' | 'B'}`
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
While `Except` solves this problem, it restricts the keys you can omit to the ones that are present in **ALL** union members, where `DistributedOmit` allows you to omit keys that are present in **ANY** union member.
|
|
41
|
+
|
|
42
|
+
@example
|
|
43
|
+
```
|
|
44
|
+
type A = {
|
|
45
|
+
discriminant: 'A';
|
|
46
|
+
foo: string;
|
|
47
|
+
a: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type B = {
|
|
51
|
+
discriminant: 'B';
|
|
52
|
+
foo: string;
|
|
53
|
+
bar: string;
|
|
54
|
+
b: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type C = {
|
|
58
|
+
discriminant: 'C';
|
|
59
|
+
bar: string;
|
|
60
|
+
c: boolean;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Notice that `foo` exists in `A` and `B`, but not in `C`, and
|
|
64
|
+
// `bar` exists in `B` and `C`, but not in `A`.
|
|
65
|
+
|
|
66
|
+
type Union = A | B | C;
|
|
67
|
+
|
|
68
|
+
type OmittedUnion = DistributedOmit<Union, 'foo' | 'bar'>;
|
|
69
|
+
|
|
70
|
+
const omittedUnion: OmittedUnion = createOmittedUnion();
|
|
71
|
+
|
|
72
|
+
if (omittedUnion.discriminant === 'A') {
|
|
73
|
+
omittedUnion.a;
|
|
74
|
+
//=> OK
|
|
75
|
+
|
|
76
|
+
omittedUnion.foo;
|
|
77
|
+
//=> Error: `foo` is not a property of `{discriminant: 'A'; a: string}`
|
|
78
|
+
|
|
79
|
+
omittedUnion.bar;
|
|
80
|
+
//=> Error: `bar` is not a property of `{discriminant: 'A'; a: string}`
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
@category Object
|
|
85
|
+
*/
|
|
86
|
+
export type DistributedOmit<ObjectType, KeyType extends KeysOfUnion<ObjectType>> =
|
|
87
|
+
ObjectType extends unknown
|
|
88
|
+
? Omit<ObjectType, KeyType>
|
|
89
|
+
: never;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type {GreaterThan} from './greater-than';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is greater than or equal to another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
GreaterThanOrEqual<1, -5>;
|
|
11
|
+
//=> true
|
|
12
|
+
|
|
13
|
+
GreaterThanOrEqual<1, 1>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
GreaterThanOrEqual<1, 5>;
|
|
17
|
+
//=> false
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: A extends B ? true : GreaterThan<A, B>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type {NumberAbsolute, And, Or, PositiveNumericStringGt} from './internal';
|
|
2
|
+
import type {IsEqual} from './is-equal';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether a given number is greater than another number.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {GreaterThan} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
GreaterThan<1, -5>;
|
|
13
|
+
//=> true
|
|
14
|
+
|
|
15
|
+
GreaterThan<1, 1>;
|
|
16
|
+
//=> false
|
|
17
|
+
|
|
18
|
+
GreaterThan<1, 5>;
|
|
19
|
+
//=> false
|
|
20
|
+
```
|
|
21
|
+
*/
|
|
22
|
+
export type GreaterThan<A extends number, B extends number> = number extends A | B
|
|
23
|
+
? never
|
|
24
|
+
: [
|
|
25
|
+
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
26
|
+
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
27
|
+
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
28
|
+
? Or<
|
|
29
|
+
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
30
|
+
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
31
|
+
> extends true
|
|
32
|
+
? true
|
|
33
|
+
: Or<
|
|
34
|
+
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
35
|
+
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
36
|
+
> extends true
|
|
37
|
+
? false
|
|
38
|
+
: true extends R[number]
|
|
39
|
+
? false
|
|
40
|
+
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
41
|
+
? [true, false] extends R
|
|
42
|
+
? false
|
|
43
|
+
: [false, true] extends R
|
|
44
|
+
? true
|
|
45
|
+
: [false, false] extends R
|
|
46
|
+
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
47
|
+
: PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
|
|
48
|
+
: never
|
|
49
|
+
: never;
|
package/source/int-range.d.ts
CHANGED
package/source/internal.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import type {Primitive} from './primitive';
|
|
|
2
2
|
import type {Simplify} from './simplify';
|
|
3
3
|
import type {Trim} from './trim';
|
|
4
4
|
import type {IsAny} from './is-any';
|
|
5
|
+
import type {NegativeInfinity, PositiveInfinity} from './numeric';
|
|
6
|
+
import type {GreaterThan} from './greater-than';
|
|
7
|
+
import type {LessThan} from './less-than';
|
|
5
8
|
import type {IsLiteral} from './is-literal';
|
|
6
9
|
import type {UnknownRecord} from './unknown-record';
|
|
7
10
|
import type {IsNever} from './is-never';
|
|
@@ -52,7 +55,7 @@ If `<Fill>` is not provided, it will default to `unknown`.
|
|
|
52
55
|
|
|
53
56
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
54
57
|
*/
|
|
55
|
-
export type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = T extends
|
|
58
|
+
export type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = T['length'] extends L
|
|
56
59
|
? T
|
|
57
60
|
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
58
61
|
|
|
@@ -87,16 +90,6 @@ Note: This type is not the return type of the `.toString()` function.
|
|
|
87
90
|
*/
|
|
88
91
|
export type ToString<T> = T extends string | number ? `${T}` : never;
|
|
89
92
|
|
|
90
|
-
/**
|
|
91
|
-
Create a tuple of length `A` and a tuple composed of two other tuples,
|
|
92
|
-
the inferred tuple `U` and a tuple of length `B`, then extracts the length of tuple `U`.
|
|
93
|
-
|
|
94
|
-
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
95
|
-
*/
|
|
96
|
-
export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
|
|
97
|
-
? ArrayLength<U>
|
|
98
|
-
: never;
|
|
99
|
-
|
|
100
93
|
/**
|
|
101
94
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
102
95
|
*/
|
|
@@ -117,6 +110,108 @@ export type IsPlainObject<T> =
|
|
|
117
110
|
? true
|
|
118
111
|
: false;
|
|
119
112
|
|
|
113
|
+
/**
|
|
114
|
+
Converts a numeric string to a number.
|
|
115
|
+
|
|
116
|
+
@example
|
|
117
|
+
```
|
|
118
|
+
type PositiveInt = StringToNumber<'1234'>;
|
|
119
|
+
//=> 1234
|
|
120
|
+
|
|
121
|
+
type NegativeInt = StringToNumber<'-1234'>;
|
|
122
|
+
//=> -1234
|
|
123
|
+
|
|
124
|
+
type PositiveFloat = StringToNumber<'1234.56'>;
|
|
125
|
+
//=> 1234.56
|
|
126
|
+
|
|
127
|
+
type NegativeFloat = StringToNumber<'-1234.56'>;
|
|
128
|
+
//=> -1234.56
|
|
129
|
+
|
|
130
|
+
type PositiveInfinity = StringToNumber<'Infinity'>;
|
|
131
|
+
//=> Infinity
|
|
132
|
+
|
|
133
|
+
type NegativeInfinity = StringToNumber<'-Infinity'>;
|
|
134
|
+
//=> -Infinity
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
@category String
|
|
138
|
+
@category Numeric
|
|
139
|
+
@category Template literal
|
|
140
|
+
*/
|
|
141
|
+
export type StringToNumber<S extends string> = S extends `${infer N extends number}`
|
|
142
|
+
? N
|
|
143
|
+
: S extends 'Infinity'
|
|
144
|
+
? PositiveInfinity
|
|
145
|
+
: S extends '-Infinity'
|
|
146
|
+
? NegativeInfinity
|
|
147
|
+
: never;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
Returns a boolean for whether the given string `S` starts with the given string `SearchString`.
|
|
151
|
+
|
|
152
|
+
@example
|
|
153
|
+
```
|
|
154
|
+
StartsWith<'abcde', 'abc'>;
|
|
155
|
+
//=> true
|
|
156
|
+
|
|
157
|
+
StartsWith<'abcde', 'bc'>;
|
|
158
|
+
//=> false
|
|
159
|
+
|
|
160
|
+
StartsWith<string, 'bc'>;
|
|
161
|
+
//=> never
|
|
162
|
+
|
|
163
|
+
StartsWith<'abcde', string>;
|
|
164
|
+
//=> never
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
@category String
|
|
168
|
+
@category Template literal
|
|
169
|
+
*/
|
|
170
|
+
export type StartsWith<S extends string, SearchString extends string> = string extends S | SearchString
|
|
171
|
+
? never
|
|
172
|
+
: S extends `${SearchString}${infer T}`
|
|
173
|
+
? true
|
|
174
|
+
: false;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
Returns the length of the given string.
|
|
178
|
+
|
|
179
|
+
@example
|
|
180
|
+
```
|
|
181
|
+
StringLength<'abcde'>;
|
|
182
|
+
//=> 5
|
|
183
|
+
|
|
184
|
+
StringLength<string>;
|
|
185
|
+
//=> never
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
@category String
|
|
189
|
+
@category Template literal
|
|
190
|
+
*/
|
|
191
|
+
export type StringLength<S extends string> = string extends S
|
|
192
|
+
? never
|
|
193
|
+
: StringToArray<S>['length'];
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
Returns an array of the characters of the string.
|
|
197
|
+
|
|
198
|
+
@example
|
|
199
|
+
```
|
|
200
|
+
StringToArray<'abcde'>;
|
|
201
|
+
//=> ['a', 'b', 'c', 'd', 'e']
|
|
202
|
+
|
|
203
|
+
StringToArray<string>;
|
|
204
|
+
//=> never
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
@category String
|
|
208
|
+
*/
|
|
209
|
+
export type StringToArray<S extends string, Result extends string[] = []> = string extends S
|
|
210
|
+
? never
|
|
211
|
+
: S extends `${infer F}${infer R}`
|
|
212
|
+
? StringToArray<R, [...Result, F]>
|
|
213
|
+
: Result;
|
|
214
|
+
|
|
120
215
|
export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
121
216
|
|
|
122
217
|
export type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
@@ -359,6 +454,190 @@ IsPrimitive<Object>
|
|
|
359
454
|
*/
|
|
360
455
|
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;
|
|
361
456
|
|
|
457
|
+
/**
|
|
458
|
+
Returns a boolean for whether A and B are both true.
|
|
459
|
+
|
|
460
|
+
@example
|
|
461
|
+
```
|
|
462
|
+
And<true, true>;
|
|
463
|
+
//=> true
|
|
464
|
+
|
|
465
|
+
And<true, false>;
|
|
466
|
+
//=> false
|
|
467
|
+
```
|
|
468
|
+
*/
|
|
469
|
+
export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
|
|
470
|
+
? true
|
|
471
|
+
: true extends [IsEqual<A, false>, IsEqual<B, false>][number]
|
|
472
|
+
? false
|
|
473
|
+
: never;
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
Returns a boolean for either A or B is true.
|
|
477
|
+
|
|
478
|
+
@example
|
|
479
|
+
```
|
|
480
|
+
Or<true, false>;
|
|
481
|
+
//=> true
|
|
482
|
+
|
|
483
|
+
Or<false, false>;
|
|
484
|
+
//=> false
|
|
485
|
+
```
|
|
486
|
+
*/
|
|
487
|
+
export type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
|
|
488
|
+
? false
|
|
489
|
+
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
|
|
490
|
+
? true
|
|
491
|
+
: never;
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
Returns a boolean for whether A is false.
|
|
495
|
+
|
|
496
|
+
@example
|
|
497
|
+
```
|
|
498
|
+
Not<true>;
|
|
499
|
+
//=> false
|
|
500
|
+
|
|
501
|
+
Not<false>;
|
|
502
|
+
//=> true
|
|
503
|
+
```
|
|
504
|
+
*/
|
|
505
|
+
export type Not<A extends boolean> = A extends true
|
|
506
|
+
? false
|
|
507
|
+
: A extends false
|
|
508
|
+
? true
|
|
509
|
+
: never;
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
Returns the maximum value from a tuple of integers.
|
|
513
|
+
|
|
514
|
+
Note:
|
|
515
|
+
- Float numbers are not supported.
|
|
516
|
+
|
|
517
|
+
@example
|
|
518
|
+
```
|
|
519
|
+
ArrayMax<[1, 2, 5, 3]>;
|
|
520
|
+
//=> 5
|
|
521
|
+
|
|
522
|
+
ArrayMax<[1, 2, 5, 3, 99, -1]>;
|
|
523
|
+
//=> 99
|
|
524
|
+
```
|
|
525
|
+
*/
|
|
526
|
+
export type ArrayMax<A extends number[], Result extends number = NegativeInfinity> = number extends A[number]
|
|
527
|
+
? never :
|
|
528
|
+
A extends [infer F extends number, ...infer R extends number[]]
|
|
529
|
+
? GreaterThan<F, Result> extends true
|
|
530
|
+
? ArrayMax<R, F>
|
|
531
|
+
: ArrayMax<R, Result>
|
|
532
|
+
: Result;
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
Returns the minimum value from a tuple of integers.
|
|
536
|
+
|
|
537
|
+
Note:
|
|
538
|
+
- Float numbers are not supported.
|
|
539
|
+
|
|
540
|
+
@example
|
|
541
|
+
```
|
|
542
|
+
ArrayMin<[1, 2, 5, 3]>;
|
|
543
|
+
//=> 1
|
|
544
|
+
|
|
545
|
+
ArrayMin<[1, 2, 5, 3, -5]>;
|
|
546
|
+
//=> -5
|
|
547
|
+
```
|
|
548
|
+
*/
|
|
549
|
+
export type ArrayMin<A extends number[], Result extends number = PositiveInfinity> = number extends A[number]
|
|
550
|
+
? never
|
|
551
|
+
: A extends [infer F extends number, ...infer R extends number[]]
|
|
552
|
+
? LessThan<F, Result> extends true
|
|
553
|
+
? ArrayMin<R, F>
|
|
554
|
+
: ArrayMin<R, Result>
|
|
555
|
+
: Result;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
Returns the absolute value of a given value.
|
|
559
|
+
|
|
560
|
+
@example
|
|
561
|
+
```
|
|
562
|
+
NumberAbsolute<-1>;
|
|
563
|
+
//=> 1
|
|
564
|
+
|
|
565
|
+
NumberAbsolute<1>;
|
|
566
|
+
//=> 1
|
|
567
|
+
|
|
568
|
+
NumberAbsolute<NegativeInfinity>
|
|
569
|
+
//=> PositiveInfinity
|
|
570
|
+
```
|
|
571
|
+
*/
|
|
572
|
+
export type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both numeric strings and have the same length.
|
|
576
|
+
|
|
577
|
+
@example
|
|
578
|
+
```
|
|
579
|
+
SameLengthPositiveNumericStringGt<'50', '10'>;
|
|
580
|
+
//=> true
|
|
581
|
+
|
|
582
|
+
SameLengthPositiveNumericStringGt<'10', '10'>;
|
|
583
|
+
//=> false
|
|
584
|
+
```
|
|
585
|
+
*/
|
|
586
|
+
type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}`
|
|
587
|
+
? B extends `${infer FirstB}${infer RestB}`
|
|
588
|
+
? FirstA extends FirstB
|
|
589
|
+
? SameLengthPositiveNumericStringGt<RestA, RestB>
|
|
590
|
+
: PositiveNumericCharacterGt<FirstA, FirstB>
|
|
591
|
+
: never
|
|
592
|
+
: false;
|
|
593
|
+
|
|
594
|
+
type NumericString = '0123456789';
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
|
|
598
|
+
|
|
599
|
+
@example
|
|
600
|
+
```
|
|
601
|
+
PositiveNumericStringGt<'500', '1'>;
|
|
602
|
+
//=> true
|
|
603
|
+
|
|
604
|
+
PositiveNumericStringGt<'1', '1'>;
|
|
605
|
+
//=> false
|
|
606
|
+
|
|
607
|
+
PositiveNumericStringGt<'1', '500'>;
|
|
608
|
+
//=> false
|
|
609
|
+
```
|
|
610
|
+
*/
|
|
611
|
+
export type PositiveNumericStringGt<A extends string, B extends string> = A extends B
|
|
612
|
+
? false
|
|
613
|
+
: [BuildTuple<StringLength<A>, 0>, BuildTuple<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]]
|
|
614
|
+
? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
|
|
615
|
+
? 0 extends Remain['length']
|
|
616
|
+
? SameLengthPositiveNumericStringGt<A, B>
|
|
617
|
+
: true
|
|
618
|
+
: false
|
|
619
|
+
: never;
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
|
|
623
|
+
|
|
624
|
+
@example
|
|
625
|
+
```
|
|
626
|
+
PositiveNumericCharacterGt<'5', '1'>;
|
|
627
|
+
//=> true
|
|
628
|
+
|
|
629
|
+
PositiveNumericCharacterGt<'1', '1'>;
|
|
630
|
+
//=> false
|
|
631
|
+
```
|
|
632
|
+
*/
|
|
633
|
+
type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
|
|
634
|
+
? NumericString extends `${infer HeadB}${B}${infer TailB}`
|
|
635
|
+
? HeadA extends `${HeadB}${infer _}${infer __}`
|
|
636
|
+
? true
|
|
637
|
+
: false
|
|
638
|
+
: never
|
|
639
|
+
: never;
|
|
640
|
+
|
|
362
641
|
/**
|
|
363
642
|
Utility type to retrieve only literal keys from type.
|
|
364
643
|
*/
|
|
@@ -506,42 +785,6 @@ Returns whether the given array `T` is readonly.
|
|
|
506
785
|
*/
|
|
507
786
|
export type IsArrayReadonly<T extends UnknownArray> = T extends unknown[] ? false : true;
|
|
508
787
|
|
|
509
|
-
/**
|
|
510
|
-
Returns the result of `A >= B`.
|
|
511
|
-
|
|
512
|
-
@example
|
|
513
|
-
```
|
|
514
|
-
type A = GTE<15, 10>;
|
|
515
|
-
//=> true
|
|
516
|
-
|
|
517
|
-
type B = GTE<10, 15>;
|
|
518
|
-
//=> false
|
|
519
|
-
|
|
520
|
-
type C = GTE<10, 10>;
|
|
521
|
-
//=> true
|
|
522
|
-
```
|
|
523
|
-
*/
|
|
524
|
-
export type GTE<A extends number, B extends number> =
|
|
525
|
-
BuildTuple<A> extends [...infer _, ...BuildTuple<B>]
|
|
526
|
-
? true
|
|
527
|
-
: false;
|
|
528
|
-
|
|
529
|
-
/**
|
|
530
|
-
Returns the result of `A > B`
|
|
531
|
-
|
|
532
|
-
@example
|
|
533
|
-
```
|
|
534
|
-
type A = GT<15, 10>;
|
|
535
|
-
//=> true
|
|
536
|
-
|
|
537
|
-
type B = GT<10, 15>;
|
|
538
|
-
//=> false
|
|
539
|
-
*/
|
|
540
|
-
export type GT<A extends number, B extends number> =
|
|
541
|
-
IsEqual<A, B> extends true
|
|
542
|
-
? false
|
|
543
|
-
: GTE<A, B>;
|
|
544
|
-
|
|
545
788
|
/**
|
|
546
789
|
Get the exact version of the given `Key` in the given object `T`.
|
|
547
790
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type {GreaterThan} from './greater-than';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is less than or equal to another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LessThanOrEqual} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
LessThanOrEqual<1, -5>;
|
|
11
|
+
//=> false
|
|
12
|
+
|
|
13
|
+
LessThanOrEqual<1, 1>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
LessThanOrEqual<1, 5>;
|
|
17
|
+
//=> true
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type LessThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: GreaterThan<A, B> extends true ? false : true;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is less than another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LessThan} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
LessThan<1, -5>;
|
|
11
|
+
//=> false
|
|
12
|
+
|
|
13
|
+
LessThan<1, 1>;
|
|
14
|
+
//=> false
|
|
15
|
+
|
|
16
|
+
LessThan<1, 5>;
|
|
17
|
+
//=> true
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type LessThan<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: GreaterThanOrEqual<A, B> extends true ? false : true;
|
package/source/numeric.d.ts
CHANGED
|
@@ -168,3 +168,20 @@ declare function setLength<T extends number>(length: NonNegativeInteger<T>): voi
|
|
|
168
168
|
@category Numeric
|
|
169
169
|
*/
|
|
170
170
|
export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
Returns a boolean for whether the given number is a negative number.
|
|
174
|
+
|
|
175
|
+
@see Negative
|
|
176
|
+
|
|
177
|
+
@example
|
|
178
|
+
```
|
|
179
|
+
import type {IsNegative} from 'type-fest';
|
|
180
|
+
|
|
181
|
+
type ShouldBeFalse = IsNegative<1>;
|
|
182
|
+
type ShouldBeTrue = IsNegative<-1>;
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
@category Numeric
|
|
186
|
+
*/
|
|
187
|
+
export type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|
package/source/omit-deep.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type {ArraySplice} from './array-splice';
|
|
2
2
|
import type {ExactKey, IsArrayReadonly, NonRecursiveType, SetArrayAccess, ToString} from './internal';
|
|
3
3
|
import type {IsEqual} from './is-equal';
|
|
4
|
+
import type {IsNever} from './is-never';
|
|
5
|
+
import type {LiteralUnion} from './literal-union';
|
|
4
6
|
import type {SimplifyDeep} from './merge-deep';
|
|
5
7
|
import type {Paths} from './paths';
|
|
6
8
|
import type {SharedUnionFieldsDeep} from './shared-union-fields-deep';
|
|
@@ -69,7 +71,7 @@ type AddressInfo = OmitDeep<Info1, 'address.1.foo'>;
|
|
|
69
71
|
@category Object
|
|
70
72
|
@category Array
|
|
71
73
|
*/
|
|
72
|
-
export type OmitDeep<T, PathUnion extends Paths<T>> =
|
|
74
|
+
export type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> =
|
|
73
75
|
SimplifyDeep<
|
|
74
76
|
SharedUnionFieldsDeep<
|
|
75
77
|
{[P in PathUnion]: OmitDeepWithOnePath<T, P>}[PathUnion]
|
|
@@ -102,9 +104,11 @@ P extends `${infer RecordKeyInPath}.${infer SubPath}`
|
|
|
102
104
|
: ObjectT[Key]
|
|
103
105
|
}
|
|
104
106
|
: ExactKey<ObjectT, P> extends infer Key
|
|
105
|
-
? Key extends
|
|
106
|
-
?
|
|
107
|
-
:
|
|
107
|
+
? IsNever<Key> extends true
|
|
108
|
+
? ObjectT
|
|
109
|
+
: Key extends PropertyKey
|
|
110
|
+
? Omit<ObjectT, Key>
|
|
111
|
+
: ObjectT
|
|
108
112
|
: ObjectT;
|
|
109
113
|
|
|
110
114
|
/**
|
|
@@ -133,4 +137,4 @@ type OmitDeepArrayWithOnePath<ArrayType extends UnknownArray, P extends string |
|
|
|
133
137
|
? []
|
|
134
138
|
// If `ArrayIndex` is a number literal
|
|
135
139
|
: ArraySplice<ArrayType, ArrayIndex, 1, [unknown]>
|
|
136
|
-
:
|
|
140
|
+
: ArrayType;
|
|
@@ -1,17 +1,53 @@
|
|
|
1
1
|
import type {IsUnknown} from './is-unknown';
|
|
2
|
+
import type {StaticPartOfArray} from './internal';
|
|
3
|
+
import type {UnknownArray} from './unknown-array';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
|
-
Create
|
|
6
|
+
Create an array that replaces the given `TArray`'s elements with the given `TObject`'s values at the given indices.
|
|
7
|
+
|
|
8
|
+
`TArray` and `TObject` supports tailing spread array like `[string, ...boolean[]]`, but does not support `[string, ...boolean[], number]`.
|
|
9
|
+
|
|
10
|
+
@example:
|
|
11
|
+
```ts
|
|
12
|
+
// object
|
|
13
|
+
type A = MergeObjectToArray<[string, number], {0: boolean}>;
|
|
14
|
+
//=> [boolean, number]
|
|
15
|
+
|
|
16
|
+
// array
|
|
17
|
+
type B = MergeObjectToArray<[string, number], [boolean]>;
|
|
18
|
+
//=> [boolean, number]
|
|
19
|
+
|
|
20
|
+
// tailing spread array
|
|
21
|
+
type C = MergeObjectToArray<[string, ...boolean[]], {1: number}>;
|
|
22
|
+
//=> [string, ...number[]]
|
|
23
|
+
|
|
24
|
+
type D = MergeObjectToArray<[string, ...boolean[]], [number, ...string[]]>;
|
|
25
|
+
//=> [number, ...string[]]
|
|
26
|
+
```
|
|
5
27
|
*/
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
28
|
+
type MergeObjectToArray<TArray extends UnknownArray, TObject, TArrayCopy extends UnknownArray = TArray> =
|
|
29
|
+
// If `TObject` is an array like `[0, 1, 2]`
|
|
30
|
+
TObject extends UnknownArray
|
|
31
|
+
// If `TObject` is a variable length array, we should use `TObject`'s type as the result type.
|
|
32
|
+
? number extends TObject['length']
|
|
33
|
+
? TObject
|
|
34
|
+
: {
|
|
35
|
+
[K in keyof TArray]: K extends keyof TObject ? TObject[K] : TArray[K]
|
|
36
|
+
}
|
|
37
|
+
: TObject extends object
|
|
38
|
+
// If `TObject` is a object witch key is number like `{0: string, 1: number}`
|
|
39
|
+
? {
|
|
40
|
+
[K in keyof TArray]:
|
|
41
|
+
K extends `${infer NumberK extends number}`
|
|
42
|
+
? NumberK extends keyof TObject ? TObject[NumberK] : TArray[K]
|
|
43
|
+
: number extends K
|
|
44
|
+
// If array key `K` is `number`, means it's a rest parameter, we should set the rest parameter type to corresponding type in `TObject`.
|
|
45
|
+
// example: `MergeObjectToParamterArray<[string, ...boolean[]], {1: number}>` => `[string, ...number[]]`
|
|
46
|
+
? StaticPartOfArray<TArrayCopy>['length'] extends keyof TObject
|
|
47
|
+
? TObject[StaticPartOfArray<TArrayCopy>['length']]
|
|
48
|
+
: TArray[K]
|
|
49
|
+
: never
|
|
50
|
+
} : never;
|
|
15
51
|
|
|
16
52
|
/**
|
|
17
53
|
Create a function that replaces some parameters with the given parameters.
|
|
@@ -39,7 +75,7 @@ Use-case:
|
|
|
39
75
|
```
|
|
40
76
|
import type {SetParameterType} from 'type-fest';
|
|
41
77
|
|
|
42
|
-
type HandleMessage = (data: Data, message: string) => void;
|
|
78
|
+
type HandleMessage = (data: Data, message: string, ...arguments: any[]) => void;
|
|
43
79
|
|
|
44
80
|
type HandleOk = SetParameterType<HandleMessage, {0: SuccessData, 1: 'ok'}>;
|
|
45
81
|
//=> type HandleOk = (data: SuccessData, message: 'ok') => void;
|
|
@@ -48,9 +84,19 @@ type HandleOk = SetParameterType<HandleMessage, {0: SuccessData, 1: 'ok'}>;
|
|
|
48
84
|
type HandleError = SetParameterType<HandleMessage, [data: ErrorData, message: 'error']>;
|
|
49
85
|
//=> type HandleError = (data: ErrorData, message: 'error') => void;
|
|
50
86
|
|
|
51
|
-
//
|
|
87
|
+
// Change single parameter type.
|
|
52
88
|
type HandleWarn = SetParameterType<HandleMessage, {1: 'warn'}>;
|
|
53
89
|
//=> type HandleWarn = (data: Data, message: 'warn') => void;
|
|
90
|
+
|
|
91
|
+
// Change rest parameter type.
|
|
92
|
+
|
|
93
|
+
// Way 1: Input full parameter type.
|
|
94
|
+
type HandleLog = SetParameterType<HandleMessage, [data: Data, message: 'log', ...arguments: string[]]>;
|
|
95
|
+
//=> type HandleLog = (data: Data, message: 'log', ...arguments: string[]) => void;
|
|
96
|
+
|
|
97
|
+
// Way 2: Input rest parameter type by Object index.
|
|
98
|
+
type HandleLog2 = SetParameterType<HandleMessage, {2: string}>;
|
|
99
|
+
//=> type HandleLog2 = (data: Data, message: string, ...arguments: string[]) => void;
|
|
54
100
|
```
|
|
55
101
|
|
|
56
102
|
@category Function
|
|
@@ -62,7 +108,7 @@ export type SetParameterType<Fn extends (...arguments_: any[]) => unknown, P ext
|
|
|
62
108
|
// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
|
|
63
109
|
// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
|
|
64
110
|
IsUnknown<ThisArg> extends true
|
|
65
|
-
? (...arguments_:
|
|
66
|
-
: (this: ThisArg, ...arguments_:
|
|
111
|
+
? (...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Fn>
|
|
112
|
+
: (this: ThisArg, ...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Fn>
|
|
67
113
|
)
|
|
68
114
|
: Fn; // This part should be unreachable
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type {Join} from './join';
|
|
2
|
+
import type {ArraySlice} from './array-slice';
|
|
3
|
+
import type {StringToArray} from './internal';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a string slice of a given range, just like `String#slice()`.
|
|
7
|
+
|
|
8
|
+
@see {ArraySlice}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {StringSlice} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
StringSlice<'abcde', 0, 2>;
|
|
15
|
+
//=> 'ab'
|
|
16
|
+
|
|
17
|
+
StringSlice<'abcde', 1>;
|
|
18
|
+
//=> 'bcde'
|
|
19
|
+
|
|
20
|
+
StringSlice<'abcde', 0, -1>;
|
|
21
|
+
//=> 'abcd'
|
|
22
|
+
|
|
23
|
+
StringSlice<'abcde', -2, -1>;
|
|
24
|
+
//=> 'd'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category String
|
|
28
|
+
*/
|
|
29
|
+
export type StringSlice<
|
|
30
|
+
S extends string,
|
|
31
|
+
Start extends number = 0,
|
|
32
|
+
End extends number = StringToArray<S>['length'],
|
|
33
|
+
> = string extends S
|
|
34
|
+
? string[]
|
|
35
|
+
: ArraySlice<StringToArray<S>, Start, End> extends infer R extends readonly string[]
|
|
36
|
+
? Join<R, ''>
|
|
37
|
+
: never;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type {NumberAbsolute, BuildTuple, And, Or} from './internal';
|
|
2
|
+
import type {IsEqual} from './is-equal';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
|
|
4
|
+
import type {LessThan} from './less-than';
|
|
5
|
+
import type {Sum} from './sum';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Returns the difference between two numbers.
|
|
9
|
+
|
|
10
|
+
Note:
|
|
11
|
+
- A or B can only support `-999` ~ `999`.
|
|
12
|
+
- If the result is negative, you can only get `number`.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {Subtract} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
Subtract<333, 222>;
|
|
19
|
+
//=> 111
|
|
20
|
+
|
|
21
|
+
Subtract<111, -222>;
|
|
22
|
+
//=> 333
|
|
23
|
+
|
|
24
|
+
Subtract<-111, 222>;
|
|
25
|
+
//=> number
|
|
26
|
+
|
|
27
|
+
Subtract<PositiveInfinity, 9999>;
|
|
28
|
+
//=> PositiveInfinity
|
|
29
|
+
|
|
30
|
+
Subtract<PositiveInfinity, PositiveInfinity>;
|
|
31
|
+
//=> number
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
@category Numeric
|
|
35
|
+
*/
|
|
36
|
+
// TODO: Support big integer and negative number.
|
|
37
|
+
export type Subtract<A extends number, B extends number> = number extends A | B
|
|
38
|
+
? number
|
|
39
|
+
: [
|
|
40
|
+
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
41
|
+
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
42
|
+
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
43
|
+
? Or<
|
|
44
|
+
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
45
|
+
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
46
|
+
> extends true
|
|
47
|
+
? PositiveInfinity
|
|
48
|
+
: Or<
|
|
49
|
+
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
50
|
+
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
51
|
+
> extends true
|
|
52
|
+
? NegativeInfinity
|
|
53
|
+
: true extends R[number]
|
|
54
|
+
? number
|
|
55
|
+
: [IsNegative<A>, IsNegative<B>] extends infer R
|
|
56
|
+
? [false, false] extends R
|
|
57
|
+
? BuildTuple<A> extends infer R
|
|
58
|
+
? R extends [...BuildTuple<B>, ...infer R]
|
|
59
|
+
? R['length']
|
|
60
|
+
: number
|
|
61
|
+
: never
|
|
62
|
+
: LessThan<A, B> extends true
|
|
63
|
+
? number
|
|
64
|
+
: [false, true] extends R
|
|
65
|
+
? Sum<A, NumberAbsolute<B>>
|
|
66
|
+
: Subtract<NumberAbsolute<B>, NumberAbsolute<A>>
|
|
67
|
+
: never
|
|
68
|
+
: never;
|
package/source/sum.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type {NumberAbsolute, BuildTuple, And, Or, ArrayMax, ArrayMin} from './internal';
|
|
2
|
+
import type {IsEqual} from './is-equal';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
|
|
4
|
+
import type {Subtract} from './subtract';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Returns the sum of two numbers.
|
|
8
|
+
|
|
9
|
+
Note:
|
|
10
|
+
- A or B can only support `-999` ~ `999`.
|
|
11
|
+
- A and B can only be small integers, less than 1000.
|
|
12
|
+
- If the result is negative, you can only get `number`.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {Sum} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
Sum<111, 222>;
|
|
19
|
+
//=> 333
|
|
20
|
+
|
|
21
|
+
Sum<-111, 222>;
|
|
22
|
+
//=> 111
|
|
23
|
+
|
|
24
|
+
Sum<111, -222>;
|
|
25
|
+
//=> number
|
|
26
|
+
|
|
27
|
+
Sum<PositiveInfinity, -9999>;
|
|
28
|
+
//=> PositiveInfinity
|
|
29
|
+
|
|
30
|
+
Sum<PositiveInfinity, NegativeInfinity>;
|
|
31
|
+
//=> number
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
@category Numeric
|
|
35
|
+
*/
|
|
36
|
+
// TODO: Support big integer and negative number.
|
|
37
|
+
export type Sum<A extends number, B extends number> = number extends A | B
|
|
38
|
+
? number
|
|
39
|
+
: [
|
|
40
|
+
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
41
|
+
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
42
|
+
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
43
|
+
? Or<
|
|
44
|
+
And<IsEqual<R[0], true>, IsEqual<R[3], false>>,
|
|
45
|
+
And<IsEqual<R[2], true>, IsEqual<R[1], false>>
|
|
46
|
+
> extends true
|
|
47
|
+
? PositiveInfinity
|
|
48
|
+
: Or<
|
|
49
|
+
And<IsEqual<R[1], true>, IsEqual<R[2], false>>,
|
|
50
|
+
And<IsEqual<R[3], true>, IsEqual<R[0], false>>
|
|
51
|
+
> extends true
|
|
52
|
+
? NegativeInfinity
|
|
53
|
+
: true extends R[number]
|
|
54
|
+
? number
|
|
55
|
+
: ([IsNegative<A>, IsNegative<B>] extends infer R
|
|
56
|
+
? [false, false] extends R
|
|
57
|
+
? [...BuildTuple<A>, ...BuildTuple<B>]['length']
|
|
58
|
+
: [true, true] extends R
|
|
59
|
+
? number
|
|
60
|
+
: ArrayMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_
|
|
61
|
+
? ArrayMin<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Min_ extends number
|
|
62
|
+
? Max_ extends A | B
|
|
63
|
+
? Subtract<Max_, Min_>
|
|
64
|
+
: number
|
|
65
|
+
: never
|
|
66
|
+
: never
|
|
67
|
+
: never) & number
|
|
68
|
+
: never;
|