@depup/type-fest 5.5.0-depup.0 → 5.10.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.
Files changed (120) hide show
  1. package/README.md +2 -2
  2. package/changes.json +1 -1
  3. package/index.d.ts +17 -8
  4. package/package.json +9 -6
  5. package/readme.md +75 -61
  6. package/source/absolute.d.ts +52 -0
  7. package/source/all-extend.d.ts +9 -5
  8. package/source/all-union-fields.d.ts +18 -18
  9. package/source/and.d.ts +1 -1
  10. package/source/array-reverse.d.ts +17 -15
  11. package/source/array-splice.d.ts +24 -24
  12. package/source/array-tail.d.ts +7 -7
  13. package/source/camel-case.d.ts +38 -5
  14. package/source/camel-cased-properties-deep.d.ts +11 -4
  15. package/source/camel-cased-properties.d.ts +5 -1
  16. package/source/conditional-keys.d.ts +1 -1
  17. package/source/delimiter-case.d.ts +11 -9
  18. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  19. package/source/delimiter-cased-properties.d.ts +5 -1
  20. package/source/empty-object.d.ts +1 -1
  21. package/source/entries.d.ts +1 -1
  22. package/source/entry.d.ts +1 -1
  23. package/source/exclude-exactly.d.ts +11 -11
  24. package/source/exclude-rest-element.d.ts +4 -4
  25. package/source/exclusify-union.d.ts +4 -4
  26. package/source/extends-strict.d.ts +129 -22
  27. package/source/extract-exactly.d.ts +56 -0
  28. package/source/get.d.ts +1 -1
  29. package/source/greater-than.d.ts +3 -2
  30. package/source/has-optional-keys.d.ts +1 -1
  31. package/source/has-readonly-keys.d.ts +1 -1
  32. package/source/has-required-keys.d.ts +1 -1
  33. package/source/has-writable-keys.d.ts +1 -1
  34. package/source/int-closed-range.d.ts +5 -4
  35. package/source/int-range.d.ts +57 -12
  36. package/source/internal/array.d.ts +22 -21
  37. package/source/internal/keys.d.ts +9 -9
  38. package/source/internal/numeric.d.ts +19 -65
  39. package/source/internal/object.d.ts +44 -6
  40. package/source/internal/string.d.ts +1 -76
  41. package/source/internal/tuple.d.ts +2 -2
  42. package/source/internal/type.d.ts +16 -10
  43. package/source/is-boolean-literal.d.ts +39 -0
  44. package/source/is-integer.d.ts +8 -8
  45. package/source/is-literal.d.ts +43 -267
  46. package/source/is-numeric-literal.d.ts +50 -0
  47. package/source/is-string-literal.d.ts +72 -0
  48. package/source/is-symbol-literal.d.ts +39 -0
  49. package/source/is-tuple.d.ts +3 -3
  50. package/source/is-union.d.ts +12 -12
  51. package/source/iterable-element.d.ts +5 -5
  52. package/source/jsonify.d.ts +5 -9
  53. package/source/kebab-case.d.ts +1 -0
  54. package/source/kebab-cased-properties-deep.d.ts +7 -0
  55. package/source/kebab-cased-properties.d.ts +5 -1
  56. package/source/keys-of-union.d.ts +2 -2
  57. package/source/last-array-element.d.ts +66 -13
  58. package/source/less-than-or-equal.d.ts +1 -1
  59. package/source/literal-to-primitive.d.ts +1 -1
  60. package/source/literal-union.d.ts +1 -1
  61. package/source/merge-deep.d.ts +3 -3
  62. package/source/merge-exclusive.d.ts +3 -3
  63. package/source/multidimensional-array.d.ts +1 -1
  64. package/source/multidimensional-readonly-array.d.ts +1 -1
  65. package/source/non-nullable-deep.d.ts +102 -0
  66. package/source/numeric.d.ts +3 -3
  67. package/source/object-merge.d.ts +21 -16
  68. package/source/omit-deep.d.ts +22 -22
  69. package/source/or.d.ts +1 -1
  70. package/source/package-json.d.ts +7 -7
  71. package/source/partial-deep.d.ts +3 -1
  72. package/source/pascal-case.d.ts +1 -0
  73. package/source/pascal-cased-properties-deep.d.ts +7 -0
  74. package/source/pascal-cased-properties.d.ts +5 -1
  75. package/source/pick-deep.d.ts +2 -0
  76. package/source/readonly-deep.d.ts +5 -3
  77. package/source/remove-prefix.d.ts +16 -34
  78. package/source/remove-suffix.d.ts +114 -0
  79. package/source/rename-keys.d.ts +163 -0
  80. package/source/replace.d.ts +2 -2
  81. package/source/require-all-or-none.d.ts +5 -5
  82. package/source/require-at-least-one.d.ts +9 -11
  83. package/source/require-exactly-one.d.ts +7 -7
  84. package/source/require-one-or-none.d.ts +5 -5
  85. package/source/required-deep.d.ts +3 -1
  86. package/source/schema.d.ts +16 -8
  87. package/source/screaming-snake-case.d.ts +1 -0
  88. package/source/set-non-nullable-deep.d.ts +8 -4
  89. package/source/set-non-nullable.d.ts +1 -1
  90. package/source/set-optional.d.ts +5 -5
  91. package/source/set-readonly.d.ts +3 -3
  92. package/source/set-required-deep.d.ts +3 -2
  93. package/source/set-required.d.ts +13 -12
  94. package/source/shared-union-fields-deep.d.ts +6 -4
  95. package/source/shared-union-fields.d.ts +9 -9
  96. package/source/snake-case.d.ts +1 -0
  97. package/source/snake-cased-properties-deep.d.ts +7 -0
  98. package/source/snake-cased-properties.d.ts +5 -1
  99. package/source/some-extend.d.ts +9 -5
  100. package/source/split-on-rest-element.d.ts +20 -17
  101. package/source/split.d.ts +22 -11
  102. package/source/string-length.d.ts +38 -0
  103. package/source/string-repeat.d.ts +48 -21
  104. package/source/string-slice.d.ts +1 -1
  105. package/source/string-to-array.d.ts +97 -0
  106. package/source/string-to-number.d.ts +67 -0
  107. package/source/subtract.d.ts +4 -3
  108. package/source/sum.d.ts +5 -4
  109. package/source/tagged.d.ts +3 -5
  110. package/source/tsconfig-json.d.ts +40 -8
  111. package/source/tuple-of.d.ts +42 -8
  112. package/source/typed-array.d.ts +6 -0
  113. package/source/union-length.d.ts +27 -0
  114. package/source/union-max.d.ts +56 -0
  115. package/source/union-to-intersection.d.ts +1 -1
  116. package/source/union-to-tuple.d.ts +8 -4
  117. package/source/unwrap-required.d.ts +37 -0
  118. package/source/words.d.ts +30 -4
  119. package/source/writable.d.ts +14 -14
  120. package/source/xor.d.ts +1 -1
@@ -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
- A stricter, non-distributive version of `extends` for checking whether one type is assignable to another.
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
- Unlike the built-in `extends` keyword, `ExtendsStrict`:
66
+ @example
67
+ ```
68
+ import type {ExtendsStrict} from 'type-fest';
8
69
 
9
- 1. Prevents distribution over union types by wrapping both types in tuples. For example, `ExtendsStrict<string | number, number>` returns `false`, whereas `string | number extends number` would result in `boolean`.
70
+ type T1 = ExtendsStrict<any, number, {strictAny: true}>;
71
+ //=> false
10
72
 
11
- 2. Treats `never` as a special case: `never` doesn't extend every other type, it only extends itself (or `any`). For example, `ExtendsStrict<never, number>` returns `false` whereas `never extends number` would result in `true`.
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<never, never>;
24
- //=> true
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
- IsAny<Left | Right> extends true
37
- ? true
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
- ? IsNever<Right>
40
- : [Left] extends [Right]
41
- ? true
42
- : false;
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
 
@@ -1,8 +1,9 @@
1
- import type {NumberAbsolute, PositiveNumericStringGt} from './internal/index.d.ts';
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<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
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
- Creates a type that represents `true` or `false` depending on whether the given type has any optional fields.
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
- Creates a type that represents `true` or `false` depending on whether the given type has any readonly fields.
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
- Creates a type that represents `true` or `false` depending on whether the given type has any required fields.
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
- Creates a type that represents `true` or `false` depending on whether the given type has any writable fields.
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,13 +2,11 @@ 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
 
11
- Note: `Start` or `End` must be non-negative and smaller than `999`.
9
+ Note: `Start` and `End` must each be between `-998` and `998`. The maximum is one less than for `IntRange` because the end is incremented internally to make it inclusive. If `Start` is greater than `End`, the result is `never`.
12
10
 
13
11
  Use-cases:
14
12
  1. This can be used to define a set of valid input/output values. for example:
@@ -25,6 +23,9 @@ type FontSize = IntClosedRange<10, 20>;
25
23
 
26
24
  type EvenNumber = IntClosedRange<0, 10, 2>;
27
25
  //=> 0 | 2 | 4 | 6 | 8 | 10
26
+
27
+ type Offset = IntClosedRange<-3, 3>;
28
+ //=> -3 | -2 | -1 | 0 | 1 | 2 | 3
28
29
  ```
29
30
 
30
31
  2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntClosedRange<0, 100>;`
@@ -1,14 +1,16 @@
1
1
  import type {TupleOf} from './tuple-of.d.ts';
2
2
  import type {Subtract} from './subtract.d.ts';
3
+ import type {Absolute} from './absolute.d.ts';
4
+ import type {IsNegative} from './numeric.d.ts';
5
+ import type {UnknownArray} from './unknown-array.d.ts';
6
+ import type {ReverseSign} from './internal/index.d.ts';
3
7
 
4
8
  /**
5
- Generate a union of numbers.
6
-
7
- The numbers are created from the given `Start` (inclusive) parameter to the given `End` (exclusive) parameter.
9
+ Generate a union of numbers between a specified start (inclusive) and end (exclusive), with an optional step.
8
10
 
9
11
  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
12
 
11
- Note: `Start` or `End` must be non-negative and smaller than `1000`.
13
+ Note: `Start` and `End` must each be between `-998` and `999`. Negative ranges reach TypeScript's instantiation-depth limit one value earlier, so their supported minimum is one smaller in magnitude than the maximum. If `Start` is greater than `End`, the result is `never`.
12
14
 
13
15
  Use-cases:
14
16
  1. This can be used to define a set of valid input/output values. for example:
@@ -25,6 +27,9 @@ type FontSize = IntRange<10, 20>;
25
27
 
26
28
  type EvenNumber = IntRange<0, 11, 2>;
27
29
  //=> 0 | 2 | 4 | 6 | 8 | 10
30
+
31
+ type Offset = IntRange<-3, 3>;
32
+ //=> -3 | -2 | -1 | 0 | 1 | 2
28
33
  ```
29
34
 
30
35
  2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntRange<0, 100>;`
@@ -42,7 +47,41 @@ type Hundreds = IntRange<100, 901, 100>;
42
47
 
43
48
  @see {@link IntClosedRange}
44
49
  */
45
- export type IntRange<Start extends number, End extends number, Step extends number = 1> = PrivateIntRange<Start, End, Step>;
50
+ export type IntRange<Start extends number, End extends number, Step extends number = 1> =
51
+ IsNegative<Start> extends true
52
+ ? PrivateNegativeIntRange<Start, End, Step>
53
+ : PrivateIntRange<Start, End, Step>;
54
+
55
+ /**
56
+ Removes `StepTuple['length']` elements from the front of `Tuple`, or empties it when it is too short.
57
+ */
58
+ type DropStep<Tuple extends UnknownArray, StepTuple extends UnknownArray> =
59
+ Tuple extends [...StepTuple, ...infer Rest extends UnknownArray] ? Rest : [];
60
+
61
+ /**
62
+ The implementation of `IntRange` for a negative `Start`.
63
+
64
+ A tuple length can never be negative, so instead of counting up to the value, `Magnitude` counts down from `-Start` and the value is `-Magnitude['length']`. Once `Magnitude` is too short to step again, the range has crossed zero and the rest is delegated to `PrivateIntRange`.
65
+ */
66
+ type PrivateNegativeIntRange<
67
+ Start extends number,
68
+ End extends number,
69
+ Step extends number,
70
+ // A `Step` below `1` cannot advance the range, so fall back to `1` like `PrivateIntRange` does
71
+ StepTuple extends UnknownArray = TupleOf<Step> extends [] ? [unknown] : TupleOf<Step>,
72
+ // How much of the range is left to generate, shortened by `Step` each time, so the range stops at `End`
73
+ Remaining extends UnknownArray = TupleOf<Subtract<End, Start>>,
74
+ // The magnitude of the current value, so the value itself is `-Magnitude['length']`
75
+ Magnitude extends UnknownArray = TupleOf<Absolute<Start>>,
76
+ Result = never,
77
+ > = Remaining extends []
78
+ ? Result
79
+ : Magnitude extends [...StepTuple, ...infer NextMagnitude extends UnknownArray]
80
+ ? PrivateNegativeIntRange<Start, End, Step, StepTuple, DropStep<Remaining, StepTuple>, NextMagnitude, Result | ReverseSign<Magnitude['length']>>
81
+ // Less than a step is left below zero, so `Crossover` is the amount the next step overshoots it by, and counting up can take over from there
82
+ : StepTuple extends [...Magnitude, ...infer Crossover extends UnknownArray]
83
+ ? Result | ReverseSign<Magnitude['length']> | PrivateIntRange<Crossover['length'], End, Step>
84
+ : never;
46
85
 
47
86
  /**
48
87
  The actual implementation of `IntRange`. It's private because it has some arguments that don't need to be exposed.
@@ -56,14 +95,20 @@ type PrivateIntRange<
56
95
  // The final `List` is `[...StartLengthTuple, ...[number, ...GapLengthTuple], ...[number, ...GapLengthTuple], ... ...]`, so can initialize the `List` with `[...StartLengthTuple]`
57
96
  List extends unknown[] = TupleOf<Start, never>,
58
97
  EndLengthTuple extends unknown[] = TupleOf<End>,
59
- > = Gap extends 0 ?
98
+ // Avoid `GreaterThan<Start, End>` here because the extra type instantiations would make `IntRange<0, 999>` exceed TypeScript's instantiation-depth limit
99
+ // `TupleOf` represents a negative length as an empty tuple, so a negative `End` must be detected before comparing the tuple lengths
100
+ // Recursive calls pass `false` because reversal only depends on the initial bounds; the termination checks below still handle reaching or overshooting `End` without repeating this tuple comparison
101
+ IsReversed extends boolean = IsNegative<End> extends true ? true : List extends [...EndLengthTuple, unknown, ...unknown[]] ? true : false,
102
+ > = IsReversed extends true
103
+ ? never
104
+ : Gap extends 0
60
105
  // Handle the case that without `Step`
61
- List['length'] extends End // The result of "List[length] === End"
62
- ? Exclude<List[number], never> // All unused elements are `never`, so exclude them
63
- : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ]>
106
+ ? List['length'] extends End // The result of "List[length] === End"
107
+ ? Exclude<List[number], never> // All unused elements are `never`, so exclude them
108
+ : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'] ], EndLengthTuple, false>
64
109
  // Handle the case that with `Step`
65
- : List extends [...(infer U), ...EndLengthTuple] // The result of "List[length] >= End", because the `...TupleOf<Gap, never>` maybe make `List` too long.
66
- ? Exclude<List[number], never>
67
- : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'], ...TupleOf<Gap, never>]>;
110
+ : List extends [...(infer U), ...EndLengthTuple] // The result of "List[length] >= End", because the `...TupleOf<Gap, never>` maybe make `List` too long.
111
+ ? Exclude<List[number], never>
112
+ : PrivateIntRange<Start, End, Step, Gap, [...List, List['length'], ...TupleOf<Gap, never>], EndLengthTuple, false>;
68
113
 
69
114
  export {};
@@ -1,6 +1,5 @@
1
1
  import type {If} from '../if.d.ts';
2
2
  import type {IsNever} from '../is-never.d.ts';
3
- import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
4
3
  import type {UnknownArray} from '../unknown-array.d.ts';
5
4
  import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts';
6
5
 
@@ -29,8 +28,8 @@ type B = StaticPartOfArray<A>;
29
28
  */
30
29
  export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
31
30
  T extends unknown
32
- ? number extends T['length'] ?
33
- T extends readonly [infer U, ...infer V]
31
+ ? number extends T['length']
32
+ ? T extends readonly [infer U, ...infer V]
34
33
  ? StaticPartOfArray<V, [...Result, U]>
35
34
  : Result
36
35
  : T
@@ -69,11 +68,11 @@ type NormalResult = SetArrayAccess<ReadonlyStringArray, false>;
69
68
  ```
70
69
  */
71
70
  export type SetArrayAccess<T extends UnknownArray, IsReadonly extends boolean> =
72
- T extends readonly [...infer U] ?
73
- IsReadonly extends true
74
- ? readonly [...U]
75
- : [...U]
76
- : T;
71
+ T extends readonly [...infer U]
72
+ ? IsReadonly extends true
73
+ ? readonly [...U]
74
+ : [...U]
75
+ : T;
77
76
 
78
77
  /**
79
78
  Returns whether the given array `T` is readonly.
@@ -111,7 +110,7 @@ type B = CollapseRestElement<[string?, string?, ...number[]]>;
111
110
  //=> [string | undefined, string | undefined, number]
112
111
  ```
113
112
  */
114
- export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray>>;
113
+ export type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, {ifNot: _CollapseRestElement<TArray>}>;
115
114
 
116
115
  type _CollapseRestElement<
117
116
  TArray extends UnknownArray,
@@ -119,26 +118,28 @@ type _CollapseRestElement<
119
118
  BackwardAccumulator extends UnknownArray = [],
120
119
  > =
121
120
  TArray extends UnknownArray // For distributing `TArray`
122
- ? keyof TArray & `${number}` extends never
123
- // Enters this branch, if `TArray` is empty (e.g., []),
124
- // or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
125
- ? TArray extends readonly [...infer Rest, infer Last]
126
- ? _CollapseRestElement<Rest, ForwardAccumulator, [Last, ...BackwardAccumulator]> // Accumulate elements that are present after the rest element.
127
- : TArray extends readonly []
128
- ? [...ForwardAccumulator, ...BackwardAccumulator]
129
- : [...ForwardAccumulator, TArray[number], ...BackwardAccumulator] // Add the rest element between the accumulated elements.
130
- : TArray extends readonly [(infer First)?, ...infer Rest]
121
+ ? '0' extends keyof TArray
122
+ // Enters this branch, if `TArray` starts with a non-rest element (e.g., `[string, number]` or `[string, ...number[]]`).
123
+ ? TArray extends readonly [(infer First)?, ...infer Rest]
131
124
  ? _CollapseRestElement<
132
125
  Rest,
133
126
  [
134
127
  ...ForwardAccumulator,
135
- '0' extends OptionalKeysOf<TArray>
136
- ? If<IsExactOptionalPropertyTypesEnabled, First, First | undefined> // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
137
- : First,
128
+ // A tuple allows fewer than one element only when its first element is optional. Written in exactly this shape because equivalent ones, such as `[] extends TArray`, are several times slower.
129
+ TArray extends readonly [unknown, ...unknown[]]
130
+ ? First
131
+ : If<IsExactOptionalPropertyTypesEnabled, First, First | undefined>, // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
138
132
  ],
139
133
  BackwardAccumulator
140
134
  >
141
135
  : never // Should never happen, since `[(infer First)?, ...infer Rest]` is a top-type for arrays.
136
+ // Enters this branch, if `TArray` is empty (e.g., []),
137
+ // or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
138
+ : TArray extends readonly [...infer Rest, infer Last]
139
+ ? _CollapseRestElement<Rest, ForwardAccumulator, [Last, ...BackwardAccumulator]> // Accumulate elements that are present after the rest element.
140
+ : TArray extends readonly []
141
+ ? [...ForwardAccumulator, ...BackwardAccumulator]
142
+ : [...ForwardAccumulator, TArray[number], ...BackwardAccumulator] // Add the rest element between the accumulated elements.
142
143
  : never; // Should never happen
143
144
 
144
145
  export {};
@@ -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
- ? 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;
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,7 @@
1
- import type {IsNever} from '../is-never.d.ts';
2
1
  import type {Finite, NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
2
  import type {UnknownArray} from '../unknown-array.d.ts';
4
- import type {StringToNumber} from './string.d.ts';
5
3
  import type {IfNotAnyOrNever, IsAnyOrNever} from './type.d.ts';
6
4
 
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
5
  /**
25
6
  Check whether the given type is a number or a number string.
26
7
 
@@ -44,11 +25,13 @@ type E = IsNumberLike<'a'>;
44
25
  //=> false
45
26
  */
46
27
  export type IsNumberLike<N> =
47
- IfNotAnyOrNever<N,
48
- N extends number | `${number}`
28
+ IfNotAnyOrNever<N, {
29
+ ifNot: N extends number | `${number}`
49
30
  ? true
50
- : false,
51
- boolean, false>;
31
+ : false;
32
+ ifAny: boolean;
33
+ ifNever: false;
34
+ }>;
52
35
 
53
36
  /**
54
37
  Returns the minimum number in the given union of numbers.
@@ -85,43 +68,6 @@ type InternalUnionMin<N extends number, T extends UnknownArray = []> =
85
68
  ? T['length']
86
69
  : InternalUnionMin<N, [...T, unknown]>;
87
70
 
88
- /**
89
- Returns the maximum number in the given union of numbers.
90
-
91
- Note: Just supports numbers from 0 to 999.
92
-
93
- @example
94
- ```
95
- type A = UnionMax<1 | 3 | 2>;
96
- //=> 3
97
-
98
- type B = UnionMax<number>;
99
- //=> number
100
-
101
- type C = UnionMax<any>;
102
- //=> any
103
-
104
- type D = UnionMax<never>;
105
- //=> never
106
- ```
107
- */
108
- export type UnionMax<N extends number> =
109
- IsAnyOrNever<N> extends true ? N
110
- : number extends N ? number
111
- : PositiveInfinity extends N ? PositiveInfinity
112
- : [N] extends [NegativeInfinity] ? NegativeInfinity
113
- : InternalUnionMax<Finite<N>>;
114
-
115
- /**
116
- The actual implementation of `UnionMax`. It's private because it has some arguments that don't need to be exposed.
117
- */
118
- type InternalUnionMax<N extends number, T extends UnknownArray = []> =
119
- IsNever<N> extends true
120
- ? T['length']
121
- : T['length'] extends N
122
- ? InternalUnionMax<Exclude<N, T['length']>, T>
123
- : InternalUnionMax<N, [...T, unknown]>;
124
-
125
71
  /**
126
72
  Returns the number with reversed sign.
127
73
 
@@ -142,10 +88,18 @@ type D = ReverseSign<PositiveInfinity>;
142
88
  */
143
89
  export type ReverseSign<N extends number> =
144
90
  // Handle edge cases
145
- N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
146
- // Handle negative numbers
147
- `${N}` extends `-${infer P extends number}` ? P
148
- // Handle positive numbers
149
- : `-${N}` extends `${infer R extends number}` ? R : never;
91
+ N extends 0
92
+ ? 0
93
+ : N extends PositiveInfinity
94
+ ? NegativeInfinity
95
+ : N extends NegativeInfinity
96
+ ? PositiveInfinity
97
+ // Handle negative numbers
98
+ : `${N}` extends `-${infer P extends number}`
99
+ ? P
100
+ // Handle positive numbers
101
+ : `-${N}` extends `${infer R extends number}`
102
+ ? R
103
+ : never;
150
104
 
151
105
  export {};