@depup/type-fest 5.5.0-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 +1 -1
- package/index.d.ts +16 -8
- package/package.json +8 -6
- package/readme.md +85 -61
- package/source/absolute.d.ts +52 -0
- package/source/all-extend.d.ts +6 -4
- package/source/all-union-fields.d.ts +18 -18
- package/source/and.d.ts +1 -1
- package/source/array-reverse.d.ts +4 -3
- package/source/array-splice.d.ts +24 -24
- 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/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 +11 -11
- 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.d.ts +3 -2
- 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 -8
- 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 +2 -2
- package/source/internal/type.d.ts +16 -10
- package/source/is-boolean-literal.d.ts +39 -0
- 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 +1 -1
- 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/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 -22
- package/source/or.d.ts +1 -1
- 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 +2 -0
- 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 +1 -1
- package/source/set-optional.d.ts +5 -5
- package/source/set-readonly.d.ts +3 -3
- package/source/set-required-deep.d.ts +3 -2
- package/source/set-required.d.ts +3 -3
- package/source/shared-union-fields-deep.d.ts +4 -3
- 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 +6 -4
- package/source/split-on-rest-element.d.ts +6 -4
- package/source/split.d.ts +1 -1
- 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 +3 -5
- 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-to-intersection.d.ts +1 -1
- package/source/union-to-tuple.d.ts +8 -4
- package/source/unwrap-required.d.ts +37 -0
- package/source/words.d.ts +30 -4
- package/source/writable.d.ts +14 -14
- package/source/xor.d.ts +1 -1
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.
|
|
@@ -82,7 +83,7 @@ export type GreaterThan<A extends number, B extends number> =
|
|
|
82
83
|
? true
|
|
83
84
|
: [false, false] extends R
|
|
84
85
|
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
85
|
-
: PositiveNumericStringGt<`${
|
|
86
|
+
: PositiveNumericStringGt<`${Absolute<B>}`, `${Absolute<A>}`>
|
|
86
87
|
: never
|
|
87
88
|
: never
|
|
88
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`
|
|
@@ -29,8 +29,8 @@ type B = StaticPartOfArray<A>;
|
|
|
29
29
|
*/
|
|
30
30
|
export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
|
|
31
31
|
T extends unknown
|
|
32
|
-
? number extends T['length']
|
|
33
|
-
T extends readonly [infer U, ...infer V]
|
|
32
|
+
? number extends T['length']
|
|
33
|
+
? T extends readonly [infer U, ...infer V]
|
|
34
34
|
? StaticPartOfArray<V, [...Result, U]>
|
|
35
35
|
: Result
|
|
36
36
|
: T
|
|
@@ -69,11 +69,11 @@ type NormalResult = SetArrayAccess<ReadonlyStringArray, false>;
|
|
|
69
69
|
```
|
|
70
70
|
*/
|
|
71
71
|
export type SetArrayAccess<T extends UnknownArray, IsReadonly extends boolean> =
|
|
72
|
-
T extends readonly [...infer U]
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
T extends readonly [...infer U]
|
|
73
|
+
? IsReadonly extends true
|
|
74
|
+
? readonly [...U]
|
|
75
|
+
: [...U]
|
|
76
|
+
: T;
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
Returns whether the given array `T` is readonly.
|
|
@@ -111,7 +111,7 @@ type B = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
|
111
111
|
//=> [string | undefined, string | undefined, number]
|
|
112
112
|
```
|
|
113
113
|
*/
|
|
114
|
-
export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray
|
|
114
|
+
export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, {ifNot: _CollapseRestElement<TArray>}>;
|
|
115
115
|
|
|
116
116
|
type _CollapseRestElement<
|
|
117
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 {};
|
|
@@ -3,13 +3,14 @@ import type {IsEqual} from '../is-equal.d.ts';
|
|
|
3
3
|
import type {KeysOfUnion} from '../keys-of-union.d.ts';
|
|
4
4
|
import type {RequiredKeysOf} from '../required-keys-of.d.ts';
|
|
5
5
|
import type {Merge} from '../merge.d.ts';
|
|
6
|
-
import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
|
|
7
6
|
import type {IsAny} from '../is-any.d.ts';
|
|
8
7
|
import type {If} from '../if.d.ts';
|
|
9
8
|
import type {IsNever} from '../is-never.d.ts';
|
|
9
|
+
import type {StringToNumber} from '../string-to-number.d.ts';
|
|
10
|
+
import type {Primitive} from '../primitive.d.ts';
|
|
10
11
|
import type {FilterDefinedKeys, FilterOptionalKeys} from './keys.d.ts';
|
|
11
|
-
import type {MapsSetsOrArrays, NonRecursiveType} from './type.d.ts';
|
|
12
|
-
import type {
|
|
12
|
+
import type {IfNotAnyOrNever, MapsSetsOrArrays, NonRecursiveType} from './type.d.ts';
|
|
13
|
+
import type {ToString} from './string.d.ts';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
Create an object type with the given key `<Key>` and value `<Value>`.
|
|
@@ -223,14 +224,23 @@ export type ApplyDefaultOptions<
|
|
|
223
224
|
Options extends object,
|
|
224
225
|
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
225
226
|
SpecifiedOptions extends Options,
|
|
227
|
+
> =
|
|
228
|
+
_ApplyDefaultOptions<Options, Defaults, SpecifiedOptions> extends infer Result extends Required<Options> // `extends Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
|
|
229
|
+
? Result
|
|
230
|
+
: never;
|
|
231
|
+
|
|
232
|
+
type _ApplyDefaultOptions<
|
|
233
|
+
Options,
|
|
234
|
+
Defaults,
|
|
235
|
+
SpecifiedOptions,
|
|
226
236
|
> =
|
|
227
237
|
If<IsAny<SpecifiedOptions>, Defaults,
|
|
228
238
|
If<IsNever<SpecifiedOptions>, Defaults,
|
|
229
|
-
|
|
239
|
+
Merge<Defaults, {
|
|
230
240
|
[Key in keyof SpecifiedOptions
|
|
231
|
-
as
|
|
241
|
+
as undefined extends Required<Options>[Key & keyof Options] ? Key : undefined extends SpecifiedOptions[Key] ? never : Key
|
|
232
242
|
]: SpecifiedOptions[Key]
|
|
233
|
-
}
|
|
243
|
+
}>>>;
|
|
234
244
|
|
|
235
245
|
/**
|
|
236
246
|
Collapses literal types in a union into their corresponding primitive types, when possible. For example, `CollapseLiterals<'foo' | 'bar' | (string & {})>` returns `string`.
|
|
@@ -265,6 +275,34 @@ export type CollapseLiterals<T> = {} extends T
|
|
|
265
275
|
? U
|
|
266
276
|
: T;
|
|
267
277
|
|
|
278
|
+
/**
|
|
279
|
+
Returns the base type of a branded type.
|
|
280
|
+
|
|
281
|
+
@example
|
|
282
|
+
```
|
|
283
|
+
type Brand = {readonly __brand: unique symbol};
|
|
284
|
+
|
|
285
|
+
type A = UnwrapBrand<string & Brand>;
|
|
286
|
+
//=> string
|
|
287
|
+
|
|
288
|
+
type B = UnwrapBrand<number & Brand>;
|
|
289
|
+
//=> number
|
|
290
|
+
|
|
291
|
+
type C = UnwrapBrand<PropertyKey & Brand>;
|
|
292
|
+
//=> PropertyKey
|
|
293
|
+
|
|
294
|
+
type D = UnwrapBrand<(1 | 200n | 'foo' | 'bar') & Brand>;
|
|
295
|
+
//=> 1 | 200n | 'foo' | 'bar'
|
|
296
|
+
```
|
|
297
|
+
*/
|
|
298
|
+
export type UnwrapBrand<T> = IfNotAnyOrNever<T, {ifNot: _UnwrapBrand<T, Primitive>}>;
|
|
299
|
+
|
|
300
|
+
type _UnwrapBrand<T, Base> = T extends Primitive
|
|
301
|
+
? T extends infer U & Pick<T, Exclude<keyof T, KeysOfUnion<Base>>>
|
|
302
|
+
? U
|
|
303
|
+
: T
|
|
304
|
+
: T;
|
|
305
|
+
|
|
268
306
|
/**
|
|
269
307
|
Normalize keys by including string and number representations wherever applicable.
|
|
270
308
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {TupleOf} from '../tuple-of.d.ts';
|
|
2
|
-
import type {NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
|
|
3
2
|
import type {Trim} from '../trim.d.ts';
|
|
3
|
+
import type {StringLength} from '../string-length.d.ts';
|
|
4
4
|
import type {Whitespace} from './characters.d.ts';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -10,42 +10,6 @@ Note: This type is not the return type of the `.toString()` function.
|
|
|
10
10
|
*/
|
|
11
11
|
export type ToString<T> = T extends string | number ? `${T}` : never;
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
Converts a numeric string to a number.
|
|
15
|
-
|
|
16
|
-
@example
|
|
17
|
-
```
|
|
18
|
-
type PositiveInt = StringToNumber<'1234'>;
|
|
19
|
-
//=> 1234
|
|
20
|
-
|
|
21
|
-
type NegativeInt = StringToNumber<'-1234'>;
|
|
22
|
-
//=> -1234
|
|
23
|
-
|
|
24
|
-
type PositiveFloat = StringToNumber<'1234.56'>;
|
|
25
|
-
//=> 1234.56
|
|
26
|
-
|
|
27
|
-
type NegativeFloat = StringToNumber<'-1234.56'>;
|
|
28
|
-
//=> -1234.56
|
|
29
|
-
|
|
30
|
-
type PositiveInfinity = StringToNumber<'Infinity'>;
|
|
31
|
-
//=> Infinity
|
|
32
|
-
|
|
33
|
-
type NegativeInfinity = StringToNumber<'-Infinity'>;
|
|
34
|
-
//=> -Infinity
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
@category String
|
|
38
|
-
@category Numeric
|
|
39
|
-
@category Template literal
|
|
40
|
-
*/
|
|
41
|
-
export type StringToNumber<S extends string> = S extends `${infer N extends number}`
|
|
42
|
-
? N
|
|
43
|
-
: S extends 'Infinity'
|
|
44
|
-
? PositiveInfinity
|
|
45
|
-
: S extends '-Infinity'
|
|
46
|
-
? NegativeInfinity
|
|
47
|
-
: never;
|
|
48
|
-
|
|
49
13
|
/**
|
|
50
14
|
Returns a boolean for whether the given string `S` starts with the given string `SearchString`.
|
|
51
15
|
|
|
@@ -73,45 +37,6 @@ export type StartsWith<S extends string, SearchString extends string> = string e
|
|
|
73
37
|
? true
|
|
74
38
|
: false;
|
|
75
39
|
|
|
76
|
-
/**
|
|
77
|
-
Returns an array of the characters of the string.
|
|
78
|
-
|
|
79
|
-
@example
|
|
80
|
-
```
|
|
81
|
-
type A = StringToArray<'abcde'>;
|
|
82
|
-
//=> ['a', 'b', 'c', 'd', 'e']
|
|
83
|
-
|
|
84
|
-
type B = StringToArray<string>;
|
|
85
|
-
//=> never
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
@category String
|
|
89
|
-
*/
|
|
90
|
-
export type StringToArray<S extends string, Result extends string[] = []> = string extends S
|
|
91
|
-
? never
|
|
92
|
-
: S extends `${infer F}${infer R}`
|
|
93
|
-
? StringToArray<R, [...Result, F]>
|
|
94
|
-
: Result;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
Returns the length of the given string.
|
|
98
|
-
|
|
99
|
-
@example
|
|
100
|
-
```
|
|
101
|
-
type A = StringLength<'abcde'>;
|
|
102
|
-
//=> 5
|
|
103
|
-
|
|
104
|
-
type B = StringLength<string>;
|
|
105
|
-
//=> never
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
@category String
|
|
109
|
-
@category Template literal
|
|
110
|
-
*/
|
|
111
|
-
export type StringLength<S extends string> = string extends S
|
|
112
|
-
? never
|
|
113
|
-
: StringToArray<S>['length'];
|
|
114
|
-
|
|
115
40
|
/**
|
|
116
41
|
Returns a boolean for whether a string is whitespace.
|
|
117
42
|
*/
|
|
@@ -46,8 +46,8 @@ type B = TupleMax<[1, 2, 5, 3, 99, -1]>;
|
|
|
46
46
|
```
|
|
47
47
|
*/
|
|
48
48
|
export type TupleMax<A extends number[], Result extends number = NegativeInfinity> = number extends A[number]
|
|
49
|
-
? never
|
|
50
|
-
A extends [infer F extends number, ...infer R extends number[]]
|
|
49
|
+
? never
|
|
50
|
+
: A extends [infer F extends number, ...infer R extends number[]]
|
|
51
51
|
? GreaterThan<F, Result> extends true
|
|
52
52
|
? TupleMax<R, F>
|
|
53
53
|
: TupleMax<R, Result>
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import type {If} from '../if.d.ts';
|
|
2
1
|
import type {IsAny} from '../is-any.d.ts';
|
|
3
2
|
import type {IsNever} from '../is-never.d.ts';
|
|
4
3
|
import type {Primitive} from '../primitive.d.ts';
|
|
5
4
|
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
6
|
-
import type {UnionToIntersection} from '../union-to-intersection.d.ts';
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
@@ -91,16 +89,16 @@ An if-else-like type that resolves depending on whether the given type is `any`
|
|
|
91
89
|
|
|
92
90
|
@example
|
|
93
91
|
```
|
|
94
|
-
// When `T` is
|
|
95
|
-
type A = IfNotAnyOrNever<string, 'VALID'
|
|
92
|
+
// When `T` is neither `any` nor `never` (like `string`) => Returns `IfNot` branch
|
|
93
|
+
type A = IfNotAnyOrNever<string, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
|
|
96
94
|
//=> 'VALID'
|
|
97
95
|
|
|
98
96
|
// When `T` is `any` => Returns `IfAny` branch
|
|
99
|
-
type B = IfNotAnyOrNever<any, 'VALID'
|
|
97
|
+
type B = IfNotAnyOrNever<any, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
|
|
100
98
|
//=> 'IS_ANY'
|
|
101
99
|
|
|
102
100
|
// When `T` is `never` => Returns `IfNever` branch
|
|
103
|
-
type C = IfNotAnyOrNever<never, 'VALID'
|
|
101
|
+
type C = IfNotAnyOrNever<never, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>;
|
|
104
102
|
//=> 'IS_NEVER'
|
|
105
103
|
```
|
|
106
104
|
|
|
@@ -113,7 +111,7 @@ import type {StringRepeat} from 'type-fest';
|
|
|
113
111
|
type NineHundredNinetyNineSpaces = StringRepeat<' ', 999>;
|
|
114
112
|
|
|
115
113
|
// The following implementation is not tail recursive
|
|
116
|
-
type TrimLeft<S extends string> = IfNotAnyOrNever<S, S extends ` ${infer R}` ? TrimLeft<R> : S>;
|
|
114
|
+
type TrimLeft<S extends string> = IfNotAnyOrNever<S, {ifNot: S extends ` ${infer R}` ? TrimLeft<R> : S}>;
|
|
117
115
|
|
|
118
116
|
// Hence, instantiations with long strings will fail
|
|
119
117
|
// @ts-expect-error
|
|
@@ -122,7 +120,7 @@ type T1 = TrimLeft<NineHundredNinetyNineSpaces>;
|
|
|
122
120
|
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
123
121
|
|
|
124
122
|
// To fix this, move the recursion into a helper type
|
|
125
|
-
type TrimLeftOptimised<S extends string> = IfNotAnyOrNever<S, _TrimLeftOptimised<S
|
|
123
|
+
type TrimLeftOptimised<S extends string> = IfNotAnyOrNever<S, {ifNot: _TrimLeftOptimised<S>}>;
|
|
126
124
|
|
|
127
125
|
type _TrimLeftOptimised<S extends string> = S extends ` ${infer R}` ? _TrimLeftOptimised<R> : S;
|
|
128
126
|
|
|
@@ -130,8 +128,16 @@ type T2 = TrimLeftOptimised<NineHundredNinetyNineSpaces>;
|
|
|
130
128
|
//=> ''
|
|
131
129
|
```
|
|
132
130
|
*/
|
|
133
|
-
export type IfNotAnyOrNever<T,
|
|
134
|
-
|
|
131
|
+
export type IfNotAnyOrNever<T, Cases extends {ifNot: unknown; ifAny?: unknown; ifNever?: unknown}> =
|
|
132
|
+
IsAny<T> extends true
|
|
133
|
+
? 'ifAny' extends keyof Cases
|
|
134
|
+
? Cases['ifAny']
|
|
135
|
+
: any
|
|
136
|
+
: IsNever<T> extends true
|
|
137
|
+
? 'ifNever' extends keyof Cases
|
|
138
|
+
? Cases['ifNever']
|
|
139
|
+
: never
|
|
140
|
+
: Cases['ifNot'];
|
|
135
141
|
|
|
136
142
|
/**
|
|
137
143
|
Returns a boolean for whether the given type is `any` or `never`.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {CollapseLiterals, UnwrapBrand} from './internal/object.d.ts';
|
|
2
|
+
import type {IfNotAnyOrNever} from './internal/type.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
6
|
+
|
|
7
|
+
@example
|
|
8
|
+
```
|
|
9
|
+
import type {IsBooleanLiteral} from 'type-fest';
|
|
10
|
+
|
|
11
|
+
type A = IsBooleanLiteral<true>;
|
|
12
|
+
//=> true
|
|
13
|
+
|
|
14
|
+
type B = IsBooleanLiteral<false>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type C = IsBooleanLiteral<boolean>;
|
|
18
|
+
//=> false
|
|
19
|
+
|
|
20
|
+
type D = IsBooleanLiteral<true | false>;
|
|
21
|
+
//=> false
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
@category Type Guard
|
|
25
|
+
@category Utilities
|
|
26
|
+
*/
|
|
27
|
+
export type IsBooleanLiteral<T> = IfNotAnyOrNever<T, {
|
|
28
|
+
ifNot: _IsBooleanLiteral<CollapseLiterals<UnwrapBrand<T>>>;
|
|
29
|
+
ifAny: false;
|
|
30
|
+
ifNever: false;
|
|
31
|
+
}>;
|
|
32
|
+
|
|
33
|
+
type _IsBooleanLiteral<T> = boolean extends T
|
|
34
|
+
? false
|
|
35
|
+
: T extends boolean
|
|
36
|
+
? true
|
|
37
|
+
: false;
|
|
38
|
+
|
|
39
|
+
export {};
|
package/source/is-integer.d.ts
CHANGED
|
@@ -47,14 +47,14 @@ type J = IsInteger<1e-7>;
|
|
|
47
47
|
@category Numeric
|
|
48
48
|
*/
|
|
49
49
|
export type IsInteger<T> =
|
|
50
|
-
T extends bigint
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
? false
|
|
55
|
-
: T extends PositiveInfinity | NegativeInfinity
|
|
50
|
+
T extends bigint
|
|
51
|
+
? true
|
|
52
|
+
: T extends number
|
|
53
|
+
? number extends T
|
|
56
54
|
? false
|
|
57
|
-
:
|
|
58
|
-
|
|
55
|
+
: T extends PositiveInfinity | NegativeInfinity
|
|
56
|
+
? false
|
|
57
|
+
: Not<IsFloat<T>>
|
|
58
|
+
: false;
|
|
59
59
|
|
|
60
60
|
export {};
|