@depup/type-fest 5.4.4-depup.0 → 5.9.0-depup.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 +2 -2
- package/changes.json +5 -0
- package/index.d.ts +23 -8
- package/package.json +17 -11
- package/readme.md +98 -64
- package/source/absolute.d.ts +52 -0
- package/source/all-extend.d.ts +8 -7
- package/source/all-union-fields.d.ts +18 -18
- package/source/and-all.d.ts +76 -0
- package/source/and.d.ts +4 -3
- package/source/array-length.d.ts +36 -0
- package/source/array-reverse.d.ts +4 -3
- package/source/array-splice.d.ts +26 -26
- package/source/array-tail.d.ts +4 -4
- package/source/camel-case.d.ts +38 -5
- package/source/camel-cased-properties-deep.d.ts +11 -4
- package/source/camel-cased-properties.d.ts +5 -1
- package/source/conditional-keys.d.ts +1 -1
- package/source/conditional-pick-deep.d.ts +5 -3
- package/source/conditional-pick.d.ts +6 -4
- package/source/delimiter-case.d.ts +11 -9
- package/source/delimiter-cased-properties-deep.d.ts +8 -1
- package/source/delimiter-cased-properties.d.ts +5 -1
- package/source/empty-object.d.ts +1 -1
- package/source/entries.d.ts +1 -1
- package/source/entry.d.ts +1 -1
- package/source/exclude-exactly.d.ts +57 -0
- package/source/exclude-rest-element.d.ts +4 -4
- package/source/exclusify-union.d.ts +4 -4
- package/source/extends-strict.d.ts +129 -22
- package/source/extract-exactly.d.ts +56 -0
- package/source/get.d.ts +1 -1
- package/source/greater-than-or-equal.d.ts +34 -1
- package/source/greater-than.d.ts +37 -3
- package/source/has-optional-keys.d.ts +1 -1
- package/source/has-readonly-keys.d.ts +1 -1
- package/source/has-required-keys.d.ts +1 -1
- package/source/has-writable-keys.d.ts +1 -1
- package/source/int-closed-range.d.ts +1 -3
- package/source/int-range.d.ts +3 -5
- package/source/internal/array.d.ts +8 -15
- package/source/internal/keys.d.ts +9 -9
- package/source/internal/numeric.d.ts +19 -27
- package/source/internal/object.d.ts +44 -6
- package/source/internal/string.d.ts +1 -76
- package/source/internal/tuple.d.ts +3 -3
- package/source/internal/type.d.ts +16 -9
- package/source/is-boolean-literal.d.ts +39 -0
- package/source/is-equal.d.ts +0 -1
- package/source/is-integer.d.ts +8 -8
- package/source/is-literal.d.ts +43 -267
- package/source/is-numeric-literal.d.ts +50 -0
- package/source/is-string-literal.d.ts +72 -0
- package/source/is-symbol-literal.d.ts +39 -0
- package/source/is-union.d.ts +12 -12
- package/source/iterable-element.d.ts +5 -5
- package/source/jsonify.d.ts +5 -9
- package/source/kebab-case.d.ts +1 -0
- package/source/kebab-cased-properties-deep.d.ts +7 -0
- package/source/kebab-cased-properties.d.ts +5 -1
- package/source/keys-of-union.d.ts +2 -2
- package/source/last-array-element.d.ts +66 -13
- package/source/less-than-or-equal.d.ts +40 -4
- package/source/less-than.d.ts +35 -3
- package/source/literal-to-primitive.d.ts +1 -1
- package/source/literal-union.d.ts +1 -1
- package/source/merge-exclusive.d.ts +3 -3
- package/source/merge.d.ts +25 -0
- package/source/multidimensional-array.d.ts +1 -1
- package/source/multidimensional-readonly-array.d.ts +1 -1
- package/source/non-nullable-deep.d.ts +102 -0
- package/source/numeric.d.ts +3 -3
- package/source/object-merge.d.ts +21 -16
- package/source/omit-deep.d.ts +22 -23
- package/source/optional.d.ts +31 -0
- package/source/or-all.d.ts +73 -0
- package/source/or.d.ts +4 -11
- package/source/package-json.d.ts +7 -7
- package/source/partial-deep.d.ts +3 -1
- package/source/pascal-case.d.ts +1 -0
- package/source/pascal-cased-properties-deep.d.ts +7 -0
- package/source/pascal-cased-properties.d.ts +5 -1
- package/source/pick-deep.d.ts +8 -21
- package/source/readonly-deep.d.ts +5 -3
- package/source/remove-prefix.d.ts +16 -34
- package/source/remove-suffix.d.ts +114 -0
- package/source/rename-keys.d.ts +163 -0
- package/source/replace.d.ts +2 -2
- package/source/require-all-or-none.d.ts +5 -5
- package/source/require-at-least-one.d.ts +9 -11
- package/source/require-exactly-one.d.ts +7 -7
- package/source/require-one-or-none.d.ts +5 -5
- package/source/required-deep.d.ts +3 -1
- package/source/schema.d.ts +16 -8
- package/source/screaming-snake-case.d.ts +1 -0
- package/source/set-non-nullable-deep.d.ts +8 -4
- package/source/set-non-nullable.d.ts +4 -11
- package/source/set-optional.d.ts +6 -10
- package/source/set-parameter-type.d.ts +2 -2
- package/source/set-readonly.d.ts +4 -8
- package/source/set-required-deep.d.ts +3 -2
- package/source/set-required.d.ts +4 -8
- package/source/shared-union-fields-deep.d.ts +5 -4
- package/source/shared-union-fields.d.ts +9 -9
- package/source/snake-case.d.ts +1 -0
- package/source/snake-cased-properties-deep.d.ts +7 -0
- package/source/snake-cased-properties.d.ts +5 -1
- package/source/some-extend.d.ts +115 -0
- package/source/split-on-rest-element.d.ts +6 -4
- package/source/split.d.ts +1 -1
- package/source/spread.d.ts +1 -5
- package/source/string-length.d.ts +38 -0
- package/source/string-repeat.d.ts +48 -21
- package/source/string-slice.d.ts +1 -1
- package/source/string-to-array.d.ts +97 -0
- package/source/string-to-number.d.ts +67 -0
- package/source/subtract.d.ts +4 -3
- package/source/sum.d.ts +5 -4
- package/source/tagged.d.ts +5 -7
- package/source/tsconfig-json.d.ts +40 -8
- package/source/tuple-of.d.ts +42 -8
- package/source/typed-array.d.ts +1 -0
- package/source/union-length.d.ts +27 -0
- package/source/union-member.d.ts +65 -0
- package/source/union-to-intersection.d.ts +1 -1
- package/source/union-to-tuple.d.ts +10 -19
- package/source/unwrap-required.d.ts +37 -0
- package/source/words.d.ts +30 -4
- package/source/writable.d.ts +15 -19
- package/source/xor.d.ts +1 -1
|
@@ -27,14 +27,14 @@ type T4 = ExcludeRestElement<[number, string]>;
|
|
|
27
27
|
@see {@link SplitOnRestElement}
|
|
28
28
|
@category Array
|
|
29
29
|
*/
|
|
30
|
-
export type ExcludeRestElement<Array_ extends UnknownArray> = IfNotAnyOrNever<Array_,
|
|
31
|
-
SplitOnRestElement<Array_> extends infer Result
|
|
30
|
+
export type ExcludeRestElement<Array_ extends UnknownArray> = IfNotAnyOrNever<Array_, {
|
|
31
|
+
ifNot: SplitOnRestElement<Array_> extends infer Result
|
|
32
32
|
? Result extends readonly UnknownArray[]
|
|
33
33
|
? IsArrayReadonly<Array_> extends true
|
|
34
34
|
? Readonly<[...Result[0], ...Result[2]]>
|
|
35
35
|
: [...Result[0], ...Result[2]]
|
|
36
36
|
: never
|
|
37
|
-
: never
|
|
38
|
-
>;
|
|
37
|
+
: never;
|
|
38
|
+
}>;
|
|
39
39
|
|
|
40
40
|
export {};
|
|
@@ -125,13 +125,13 @@ type D = ExclusifyUnion<{a?: 1; readonly b: 2} | {d: 4}>;
|
|
|
125
125
|
@category Object
|
|
126
126
|
@category Union
|
|
127
127
|
*/
|
|
128
|
-
export type ExclusifyUnion<Union> = IfNotAnyOrNever<Union,
|
|
129
|
-
If<IsUnknown<Union>, Union,
|
|
128
|
+
export type ExclusifyUnion<Union> = IfNotAnyOrNever<Union, {
|
|
129
|
+
ifNot: If<IsUnknown<Union>, Union,
|
|
130
130
|
Extract<Union, NonRecursiveType | MapsSetsOrArrays> extends infer SkippedMembers
|
|
131
131
|
? SkippedMembers | _ExclusifyUnion<Exclude<Union, SkippedMembers>>
|
|
132
132
|
: never
|
|
133
|
-
|
|
134
|
-
>;
|
|
133
|
+
>;
|
|
134
|
+
}>;
|
|
135
135
|
|
|
136
136
|
type _ExclusifyUnion<Union, UnionCopy = Union> = Union extends unknown // For distributing `Union`
|
|
137
137
|
? Simplify<
|
|
@@ -1,44 +1,151 @@
|
|
|
1
1
|
import type {IsNever} from './is-never.d.ts';
|
|
2
2
|
import type {IsAny} from './is-any.d.ts';
|
|
3
|
+
import type {ApplyDefaultOptions} from './internal/object.d.ts';
|
|
4
|
+
import type {And} from './and.d.ts';
|
|
5
|
+
import type {Or} from './or.d.ts';
|
|
6
|
+
import type {IsUnknown} from './is-unknown.d.ts';
|
|
7
|
+
import type {OrAll} from './or-all.d.ts';
|
|
3
8
|
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
export type ExtendsStrictOptions = {
|
|
10
|
+
/**
|
|
11
|
+
Whether to distribute over unions.
|
|
12
|
+
|
|
13
|
+
@default false
|
|
14
|
+
|
|
15
|
+
@example
|
|
16
|
+
```
|
|
17
|
+
import type {ExtendsStrict} from 'type-fest';
|
|
18
|
+
|
|
19
|
+
type T1 = ExtendsStrict<string | number, string, {distributiveUnions: true}>;
|
|
20
|
+
//=> boolean
|
|
21
|
+
|
|
22
|
+
type T2 = ExtendsStrict<string | number, string, {distributiveUnions: false}>;
|
|
23
|
+
//=> false
|
|
24
|
+
```
|
|
25
|
+
*/
|
|
26
|
+
distributiveUnions?: boolean;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
Whether `never` extends every other type.
|
|
30
|
+
|
|
31
|
+
When enabled, `never` is not treated as a bottom type and only extends itself (or `any` / `unknown`).
|
|
32
|
+
|
|
33
|
+
@default true
|
|
34
|
+
|
|
35
|
+
@example
|
|
36
|
+
```
|
|
37
|
+
import type {ExtendsStrict} from 'type-fest';
|
|
38
|
+
|
|
39
|
+
type T1 = ExtendsStrict<never, number, {strictNever: true}>;
|
|
40
|
+
//=> false
|
|
41
|
+
|
|
42
|
+
type T2 = ExtendsStrict<never, number, {strictNever: false}>;
|
|
43
|
+
//=> true
|
|
44
|
+
|
|
45
|
+
type T3 = ExtendsStrict<never, never, {strictNever: true}>;
|
|
46
|
+
//=> true
|
|
47
|
+
|
|
48
|
+
type T4 = ExtendsStrict<never, any, {strictNever: true}>;
|
|
49
|
+
//=> true
|
|
50
|
+
|
|
51
|
+
type T5 = ExtendsStrict<never, unknown, {strictNever: true}>;
|
|
52
|
+
//=> true
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Note: This option only has an effect when checking assignability from `never` (`ExtendsStrict<never, ...>`), and not when checking assignability to `never` (`ExtendsStrict<..., never>`).
|
|
56
|
+
*/
|
|
57
|
+
strictNever?: boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
Whether `any` extends every other type.
|
|
61
|
+
|
|
62
|
+
When enabled, `any` does not extend every other type, it only extends itself (or `unknown`).
|
|
63
|
+
|
|
64
|
+
@default false
|
|
6
65
|
|
|
7
|
-
|
|
66
|
+
@example
|
|
67
|
+
```
|
|
68
|
+
import type {ExtendsStrict} from 'type-fest';
|
|
8
69
|
|
|
9
|
-
|
|
70
|
+
type T1 = ExtendsStrict<any, number, {strictAny: true}>;
|
|
71
|
+
//=> false
|
|
10
72
|
|
|
11
|
-
|
|
73
|
+
type T2 = ExtendsStrict<any, number, {strictAny: false; distributiveUnions: false}>;
|
|
74
|
+
//=> true
|
|
75
|
+
|
|
76
|
+
type T3 = ExtendsStrict<any, any, {strictAny: true}>;
|
|
77
|
+
//=> true
|
|
78
|
+
|
|
79
|
+
type T4 = ExtendsStrict<any, unknown, {strictAny: true}>;
|
|
80
|
+
//=> true
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Note: If `strictAny` is `false` and `distributiveUnions` is `true`, then `any` would distribute and the result would be the `boolean` type, except when checking assignability to `any` or `unknown`.
|
|
84
|
+
|
|
85
|
+
@example
|
|
86
|
+
```
|
|
87
|
+
import type {ExtendsStrict} from 'type-fest';
|
|
88
|
+
|
|
89
|
+
type T1 = ExtendsStrict<any, number, {strictAny: false; distributiveUnions: true}>;
|
|
90
|
+
//=> boolean
|
|
91
|
+
|
|
92
|
+
type T2 = ExtendsStrict<any, any, {strictAny: false; distributiveUnions: true}>;
|
|
93
|
+
//=> true
|
|
94
|
+
|
|
95
|
+
type T3 = ExtendsStrict<any, unknown, {strictAny: false; distributiveUnions: true}>;
|
|
96
|
+
//=> true
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Note: This option only has an effect when checking assignability from `any` (`ExtendsStrict<any, ...>`), and not when checking assignability to `any` (`ExtendsStrict<..., any>`).
|
|
100
|
+
*/
|
|
101
|
+
strictAny?: boolean;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
type DefaultExtendsStrictOptions = {
|
|
105
|
+
distributiveUnions: false;
|
|
106
|
+
strictNever: true;
|
|
107
|
+
strictAny: false;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
A customizable version of `extends` for checking whether one type is assignable to another.
|
|
112
|
+
|
|
113
|
+
Refer {@link ExtendsStrictOptions} for the different ways you can customize the behavior of this type.
|
|
12
114
|
|
|
13
115
|
@example
|
|
14
116
|
```
|
|
15
117
|
import type {ExtendsStrict} from 'type-fest';
|
|
16
118
|
|
|
17
|
-
type T1 = ExtendsStrict<number | string, string>;
|
|
119
|
+
type T1 = ExtendsStrict<number | string, string, {distributiveUnions: false}>;
|
|
18
120
|
//=> false
|
|
19
121
|
|
|
20
|
-
type T2 = ExtendsStrict<never, number>;
|
|
122
|
+
type T2 = ExtendsStrict<never, number, {strictNever: true}>;
|
|
21
123
|
//=> false
|
|
22
124
|
|
|
23
|
-
type T3 = ExtendsStrict<
|
|
24
|
-
//=>
|
|
25
|
-
|
|
26
|
-
type T4 = ExtendsStrict<string, number | string>;
|
|
27
|
-
//=> true
|
|
28
|
-
|
|
29
|
-
type T5 = ExtendsStrict<string, string>;
|
|
30
|
-
//=> true
|
|
125
|
+
type T3 = ExtendsStrict<any, number, {strictAny: true}>;
|
|
126
|
+
//=> false
|
|
31
127
|
```
|
|
32
128
|
|
|
129
|
+
@see {@link ExtendsStrictOptions}
|
|
130
|
+
|
|
33
131
|
@category Improved Built-in
|
|
34
132
|
*/
|
|
35
|
-
export type ExtendsStrict<Left, Right> =
|
|
36
|
-
|
|
37
|
-
|
|
133
|
+
export type ExtendsStrict<Left, Right, Options extends ExtendsStrictOptions = {}> =
|
|
134
|
+
_ExtendsStrict<Left, Right, ApplyDefaultOptions<ExtendsStrictOptions, DefaultExtendsStrictOptions, Options>>;
|
|
135
|
+
|
|
136
|
+
type _ExtendsStrict<Left, Right, Options extends Required<ExtendsStrictOptions>> =
|
|
137
|
+
And<IsAny<Left>, Options['strictAny']> extends true
|
|
138
|
+
? Or<IsAny<Right>, IsUnknown<Right>>
|
|
38
139
|
: IsNever<Left> extends true
|
|
39
|
-
?
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
140
|
+
? Options['strictNever'] extends true
|
|
141
|
+
? OrAll<[IsNever<Right>, IsAny<Right>, IsUnknown<Right>]>
|
|
142
|
+
: true
|
|
143
|
+
: Options['distributiveUnions'] extends true
|
|
144
|
+
? Left extends Right
|
|
145
|
+
? true
|
|
146
|
+
: false
|
|
147
|
+
: [Left] extends [Right]
|
|
148
|
+
? true
|
|
149
|
+
: false;
|
|
43
150
|
|
|
44
151
|
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type {IsAny} from './is-any.d.ts';
|
|
2
|
+
import type {If} from './if.d.ts';
|
|
3
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
4
|
+
import type {IfNotAnyOrNever} from './internal/type.d.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
A stricter version of `Extract<T, U>` that extracts types only when they are exactly identical.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {ExtractExactly} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type TestExtract1 = Extract<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
|
|
14
|
+
//=> 'a' | 'b' | 'c'
|
|
15
|
+
|
|
16
|
+
type TestExtractExactly1 = ExtractExactly<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
|
|
17
|
+
//=> never
|
|
18
|
+
|
|
19
|
+
type TestExtract2 = Extract<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
|
|
20
|
+
//=> 'a' | 'b' | 'c' | 1 | 2 | 3
|
|
21
|
+
|
|
22
|
+
type TestExtractExactly2 = ExtractExactly<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
|
|
23
|
+
//=> never
|
|
24
|
+
|
|
25
|
+
type TestExtract3 = Extract<{a: string} | {a: string; b: string}, {a: string}>;
|
|
26
|
+
//=> {a: string} | {a: string; b: string}
|
|
27
|
+
|
|
28
|
+
type TestExtractExactly3 = ExtractExactly<{a: string} | {a: string; b: string}, {a: string}>;
|
|
29
|
+
//=> {a: string}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
@category Improved Built-in
|
|
33
|
+
*/
|
|
34
|
+
export type ExtractExactly<Union, Match> =
|
|
35
|
+
IfNotAnyOrNever<Union, {
|
|
36
|
+
ifNot: _ExtractExactly<Union, Match>;
|
|
37
|
+
// If `Union` is `any`, then if `Match` is `any`, return `any`, else return `never`.
|
|
38
|
+
ifAny: If<IsAny<Match>, Union, never>;
|
|
39
|
+
// If `Union` is `never`, return `never`, doesn't matter what `Match` is.
|
|
40
|
+
ifNever: never;
|
|
41
|
+
}>;
|
|
42
|
+
|
|
43
|
+
type _ExtractExactly<Union, Match> =
|
|
44
|
+
IfNotAnyOrNever<Match, {
|
|
45
|
+
ifNot: Union extends unknown // For distributing `Union`
|
|
46
|
+
? [Match extends unknown // For distributing `Match`
|
|
47
|
+
? If<IsEqual<Union, Match>, true, never>
|
|
48
|
+
: never] extends [never] ? never : Union
|
|
49
|
+
: never;
|
|
50
|
+
// If `Match` is `any` or `never`, then return `never`,
|
|
51
|
+
// because `Union` cannot be `any` or `never` here.
|
|
52
|
+
ifAny: never;
|
|
53
|
+
ifNever: never;
|
|
54
|
+
}>;
|
|
55
|
+
|
|
56
|
+
export {};
|
package/source/get.d.ts
CHANGED
|
@@ -161,7 +161,7 @@ type PropertyOf<BaseType, Key extends string, Options extends Required<GetOption
|
|
|
161
161
|
|
|
162
162
|
// This works by first splitting the path based on `.` and `[...]` characters into a tuple of string keys. Then it recursively uses the head key to get the next property of the current object, until there are no keys left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples and dictionaries with number keys.
|
|
163
163
|
/**
|
|
164
|
-
Get a deeply-nested property from an object using a key path, like Lodash's `.get()` function.
|
|
164
|
+
Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs#get) function.
|
|
165
165
|
|
|
166
166
|
Use-case: Retrieve a property from deep inside an API response or some other complex object.
|
|
167
167
|
|
|
@@ -16,9 +16,42 @@ type B = GreaterThanOrEqual<1, 1>;
|
|
|
16
16
|
type C = GreaterThanOrEqual<1, 5>;
|
|
17
17
|
//=> false
|
|
18
18
|
```
|
|
19
|
+
|
|
20
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
type A = GreaterThanOrEqual<number, 1>;
|
|
27
|
+
//=> boolean
|
|
28
|
+
|
|
29
|
+
type B = GreaterThanOrEqual<1, number>;
|
|
30
|
+
//=> boolean
|
|
31
|
+
|
|
32
|
+
type C = GreaterThanOrEqual<number, number>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@example
|
|
37
|
+
```
|
|
38
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
39
|
+
|
|
40
|
+
// Use `GreaterThanOrEqual` to constrain a function parameter to non-negative numbers.
|
|
41
|
+
declare function setNonNegative<N extends number>(value: GreaterThanOrEqual<N, 0> extends true ? N : never): void;
|
|
42
|
+
|
|
43
|
+
setNonNegative(0); // ✅ Allowed
|
|
44
|
+
setNonNegative(1); // ✅ Allowed
|
|
45
|
+
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
setNonNegative(-1);
|
|
48
|
+
|
|
49
|
+
// @ts-expect-error
|
|
50
|
+
setNonNegative(-2);
|
|
51
|
+
```
|
|
19
52
|
*/
|
|
20
53
|
export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
-
?
|
|
54
|
+
? boolean
|
|
22
55
|
: A extends number // For distributing `A`
|
|
23
56
|
? B extends number // For distributing `B`
|
|
24
57
|
? A extends B
|
package/source/greater-than.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {PositiveNumericStringGt} from './internal/index.d.ts';
|
|
2
2
|
import type {IsEqual} from './is-equal.d.ts';
|
|
3
3
|
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
|
|
4
4
|
import type {And} from './and.d.ts';
|
|
5
5
|
import type {Or} from './or.d.ts';
|
|
6
|
+
import type {Absolute} from './absolute.d.ts';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
Returns a boolean for whether a given number is greater than another number.
|
|
@@ -20,12 +21,45 @@ type B = GreaterThan<1, 1>;
|
|
|
20
21
|
type C = GreaterThan<1, 5>;
|
|
21
22
|
//=> false
|
|
22
23
|
```
|
|
24
|
+
|
|
25
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
26
|
+
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import type {GreaterThan} from 'type-fest';
|
|
30
|
+
|
|
31
|
+
type A = GreaterThan<number, 1>;
|
|
32
|
+
//=> boolean
|
|
33
|
+
|
|
34
|
+
type B = GreaterThan<1, number>;
|
|
35
|
+
//=> boolean
|
|
36
|
+
|
|
37
|
+
type C = GreaterThan<number, number>;
|
|
38
|
+
//=> boolean
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
@example
|
|
42
|
+
```
|
|
43
|
+
import type {GreaterThan} from 'type-fest';
|
|
44
|
+
|
|
45
|
+
// Use `GreaterThan` to constrain a function parameter to positive numbers.
|
|
46
|
+
declare function setPositive<N extends number>(value: GreaterThan<N, 0> extends true ? N : never): void;
|
|
47
|
+
|
|
48
|
+
setPositive(1); // ✅ Allowed
|
|
49
|
+
setPositive(2); // ✅ Allowed
|
|
50
|
+
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
setPositive(0);
|
|
53
|
+
|
|
54
|
+
// @ts-expect-error
|
|
55
|
+
setPositive(-1);
|
|
56
|
+
```
|
|
23
57
|
*/
|
|
24
58
|
export type GreaterThan<A extends number, B extends number> =
|
|
25
59
|
A extends number // For distributing `A`
|
|
26
60
|
? B extends number // For distributing `B`
|
|
27
61
|
? number extends A | B
|
|
28
|
-
?
|
|
62
|
+
? boolean
|
|
29
63
|
: [
|
|
30
64
|
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
31
65
|
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
@@ -49,7 +83,7 @@ export type GreaterThan<A extends number, B extends number> =
|
|
|
49
83
|
? true
|
|
50
84
|
: [false, false] extends R
|
|
51
85
|
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
52
|
-
: PositiveNumericStringGt<`${
|
|
86
|
+
: PositiveNumericStringGt<`${Absolute<B>}`, `${Absolute<A>}`>
|
|
53
87
|
: never
|
|
54
88
|
: never
|
|
55
89
|
: never // Should never happen
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {OptionalKeysOf} from './optional-keys-of.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
|
|
4
|
+
Returns a boolean for whether the given type has any optional fields.
|
|
5
5
|
|
|
6
6
|
This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {ReadonlyKeysOf} from './readonly-keys-of.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
|
|
4
|
+
Returns a boolean for whether the given type has any readonly fields.
|
|
5
5
|
|
|
6
6
|
This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {RequiredKeysOf} from './required-keys-of.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
|
|
4
|
+
Returns a boolean for whether the given type has any required fields.
|
|
5
5
|
|
|
6
6
|
This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {WritableKeysOf} from './writable-keys-of.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
|
|
4
|
+
Returns a boolean for whether the given type has any writable fields.
|
|
5
5
|
|
|
6
6
|
This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
|
|
7
7
|
|
|
@@ -2,9 +2,7 @@ import type {IntRange} from './int-range.d.ts';
|
|
|
2
2
|
import type {Sum} from './sum.d.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
Generate a union of numbers.
|
|
6
|
-
|
|
7
|
-
The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
|
|
5
|
+
Generate a union of numbers between a specified start and end (both inclusive), with an optional step.
|
|
8
6
|
|
|
9
7
|
You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntClosedRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8 | 10`.
|
|
10
8
|
|
package/source/int-range.d.ts
CHANGED
|
@@ -2,9 +2,7 @@ import type {TupleOf} from './tuple-of.d.ts';
|
|
|
2
2
|
import type {Subtract} from './subtract.d.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
Generate a union of numbers.
|
|
6
|
-
|
|
7
|
-
The numbers are created from the given `Start` (inclusive) parameter to the given `End` (exclusive) parameter.
|
|
5
|
+
Generate a union of numbers between a specified start (inclusive) and end (exclusive), with an optional step.
|
|
8
6
|
|
|
9
7
|
You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8`.
|
|
10
8
|
|
|
@@ -56,9 +54,9 @@ type PrivateIntRange<
|
|
|
56
54
|
// The final `List` is `[...StartLengthTuple, ...[number, ...GapLengthTuple], ...[number, ...GapLengthTuple], ... ...]`, so can initialize the `List` with `[...StartLengthTuple]`
|
|
57
55
|
List extends unknown[] = TupleOf<Start, never>,
|
|
58
56
|
EndLengthTuple extends unknown[] = TupleOf<End>,
|
|
59
|
-
> = Gap extends 0
|
|
57
|
+
> = Gap extends 0
|
|
60
58
|
// Handle the case that without `Step`
|
|
61
|
-
List['length'] extends End // The result of "List[length] === End"
|
|
59
|
+
? List['length'] extends End // The result of "List[length] === End"
|
|
62
60
|
? Exclude<List[number], never> // All unused elements are `never`, so exclude them
|
|
63
61
|
: PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ]>
|
|
64
62
|
// Handle the case that with `Step`
|
|
@@ -4,13 +4,6 @@ import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
|
|
|
4
4
|
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
5
5
|
import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
Infer the length of the given array `<T>`.
|
|
9
|
-
|
|
10
|
-
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
11
|
-
*/
|
|
12
|
-
type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
|
|
13
|
-
|
|
14
7
|
/**
|
|
15
8
|
Matches any unknown array or tuple.
|
|
16
9
|
*/
|
|
@@ -36,8 +29,8 @@ type B = StaticPartOfArray<A>;
|
|
|
36
29
|
*/
|
|
37
30
|
export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
|
|
38
31
|
T extends unknown
|
|
39
|
-
? number extends T['length']
|
|
40
|
-
T extends readonly [infer U, ...infer V]
|
|
32
|
+
? number extends T['length']
|
|
33
|
+
? T extends readonly [infer U, ...infer V]
|
|
41
34
|
? StaticPartOfArray<V, [...Result, U]>
|
|
42
35
|
: Result
|
|
43
36
|
: T
|
|
@@ -76,11 +69,11 @@ type NormalResult = SetArrayAccess<ReadonlyStringArray, false>;
|
|
|
76
69
|
```
|
|
77
70
|
*/
|
|
78
71
|
export type SetArrayAccess<T extends UnknownArray, IsReadonly extends boolean> =
|
|
79
|
-
T extends readonly [...infer U]
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
72
|
+
T extends readonly [...infer U]
|
|
73
|
+
? IsReadonly extends true
|
|
74
|
+
? readonly [...U]
|
|
75
|
+
: [...U]
|
|
76
|
+
: T;
|
|
84
77
|
|
|
85
78
|
/**
|
|
86
79
|
Returns whether the given array `T` is readonly.
|
|
@@ -118,7 +111,7 @@ type B = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
|
118
111
|
//=> [string | undefined, string | undefined, number]
|
|
119
112
|
```
|
|
120
113
|
*/
|
|
121
|
-
export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray
|
|
114
|
+
export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, {ifNot: _CollapseRestElement<TArray>}>;
|
|
122
115
|
|
|
123
116
|
type _CollapseRestElement<
|
|
124
117
|
TArray extends UnknownArray,
|
|
@@ -87,14 +87,14 @@ type Key4 = ExactKey<Object, 1>;
|
|
|
87
87
|
@category Object
|
|
88
88
|
*/
|
|
89
89
|
export type ExactKey<T extends object, Key extends PropertyKey> =
|
|
90
|
-
Key extends keyof T
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
Key extends keyof T
|
|
91
|
+
? Key
|
|
92
|
+
: ToString<Key> extends keyof T
|
|
93
|
+
? ToString<Key>
|
|
94
|
+
: Key extends `${infer NumberKey extends number}`
|
|
95
|
+
? NumberKey extends keyof T
|
|
96
|
+
? NumberKey
|
|
97
|
+
: never
|
|
98
|
+
: never;
|
|
99
99
|
|
|
100
100
|
export {};
|
|
@@ -1,26 +1,8 @@
|
|
|
1
1
|
import type {IsNever} from '../is-never.d.ts';
|
|
2
2
|
import type {Finite, NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
|
|
3
3
|
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
4
|
-
import type {StringToNumber} from './string.d.ts';
|
|
5
4
|
import type {IfNotAnyOrNever, IsAnyOrNever} from './type.d.ts';
|
|
6
5
|
|
|
7
|
-
/**
|
|
8
|
-
Returns the absolute value of a given value.
|
|
9
|
-
|
|
10
|
-
@example
|
|
11
|
-
```
|
|
12
|
-
type A = NumberAbsolute<-1>;
|
|
13
|
-
//=> 1
|
|
14
|
-
|
|
15
|
-
type B = NumberAbsolute<1>;
|
|
16
|
-
//=> 1
|
|
17
|
-
|
|
18
|
-
type C = NumberAbsolute<NegativeInfinity>;
|
|
19
|
-
//=> PositiveInfinity
|
|
20
|
-
```
|
|
21
|
-
*/
|
|
22
|
-
export type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
|
|
23
|
-
|
|
24
6
|
/**
|
|
25
7
|
Check whether the given type is a number or a number string.
|
|
26
8
|
|
|
@@ -44,11 +26,13 @@ type E = IsNumberLike<'a'>;
|
|
|
44
26
|
//=> false
|
|
45
27
|
*/
|
|
46
28
|
export type IsNumberLike<N> =
|
|
47
|
-
IfNotAnyOrNever<N,
|
|
48
|
-
N extends number | `${number}`
|
|
29
|
+
IfNotAnyOrNever<N, {
|
|
30
|
+
ifNot: N extends number | `${number}`
|
|
49
31
|
? true
|
|
50
|
-
: false
|
|
51
|
-
boolean
|
|
32
|
+
: false;
|
|
33
|
+
ifAny: boolean;
|
|
34
|
+
ifNever: false;
|
|
35
|
+
}>;
|
|
52
36
|
|
|
53
37
|
/**
|
|
54
38
|
Returns the minimum number in the given union of numbers.
|
|
@@ -142,10 +126,18 @@ type D = ReverseSign<PositiveInfinity>;
|
|
|
142
126
|
*/
|
|
143
127
|
export type ReverseSign<N extends number> =
|
|
144
128
|
// Handle edge cases
|
|
145
|
-
N extends 0
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
129
|
+
N extends 0
|
|
130
|
+
? 0
|
|
131
|
+
: N extends PositiveInfinity
|
|
132
|
+
? NegativeInfinity
|
|
133
|
+
: N extends NegativeInfinity
|
|
134
|
+
? PositiveInfinity
|
|
135
|
+
// Handle negative numbers
|
|
136
|
+
: `${N}` extends `-${infer P extends number}`
|
|
137
|
+
? P
|
|
138
|
+
// Handle positive numbers
|
|
139
|
+
: `-${N}` extends `${infer R extends number}`
|
|
140
|
+
? R
|
|
141
|
+
: never;
|
|
150
142
|
|
|
151
143
|
export {};
|