@depup/type-fest 5.4.4-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 (201) hide show
  1. package/README.md +25 -0
  2. package/index.d.ts +211 -0
  3. package/license-cc0 +121 -0
  4. package/license-mit +9 -0
  5. package/package.json +85 -0
  6. package/readme.md +1052 -0
  7. package/source/all-extend.d.ts +120 -0
  8. package/source/all-union-fields.d.ts +91 -0
  9. package/source/and.d.ts +81 -0
  10. package/source/array-element.d.ts +46 -0
  11. package/source/array-indices.d.ts +25 -0
  12. package/source/array-reverse.d.ts +84 -0
  13. package/source/array-slice.d.ts +132 -0
  14. package/source/array-splice.d.ts +104 -0
  15. package/source/array-tail.d.ts +70 -0
  16. package/source/array-values.d.ts +24 -0
  17. package/source/arrayable.d.ts +31 -0
  18. package/source/async-return-type.d.ts +28 -0
  19. package/source/asyncify.d.ts +25 -0
  20. package/source/basic.d.ts +38 -0
  21. package/source/camel-case.d.ts +92 -0
  22. package/source/camel-cased-properties-deep.d.ts +99 -0
  23. package/source/camel-cased-properties.d.ts +45 -0
  24. package/source/characters.d.ts +67 -0
  25. package/source/conditional-except.d.ts +47 -0
  26. package/source/conditional-keys.d.ts +63 -0
  27. package/source/conditional-pick-deep.d.ts +120 -0
  28. package/source/conditional-pick.d.ts +46 -0
  29. package/source/conditional-simplify-deep.d.ts +73 -0
  30. package/source/conditional-simplify.d.ts +50 -0
  31. package/source/delimiter-case.d.ts +77 -0
  32. package/source/delimiter-cased-properties-deep.d.ts +108 -0
  33. package/source/delimiter-cased-properties.d.ts +48 -0
  34. package/source/distributed-omit.d.ts +96 -0
  35. package/source/distributed-pick.d.ts +92 -0
  36. package/source/empty-object.d.ts +51 -0
  37. package/source/entries.d.ts +64 -0
  38. package/source/entry.d.ts +67 -0
  39. package/source/exact.d.ts +73 -0
  40. package/source/except.d.ts +112 -0
  41. package/source/exclude-rest-element.d.ts +40 -0
  42. package/source/exclude-strict.d.ts +51 -0
  43. package/source/exclusify-union.d.ts +147 -0
  44. package/source/extends-strict.d.ts +44 -0
  45. package/source/extract-rest-element.d.ts +30 -0
  46. package/source/extract-strict.d.ts +51 -0
  47. package/source/find-global-type.d.ts +68 -0
  48. package/source/fixed-length-array.d.ts +97 -0
  49. package/source/get.d.ts +227 -0
  50. package/source/global-this.d.ts +24 -0
  51. package/source/globals/index.d.ts +3 -0
  52. package/source/globals/observable-like.d.ts +78 -0
  53. package/source/greater-than-or-equal.d.ts +30 -0
  54. package/source/greater-than.d.ts +58 -0
  55. package/source/has-optional-keys.d.ts +23 -0
  56. package/source/has-readonly-keys.d.ts +23 -0
  57. package/source/has-required-keys.d.ts +61 -0
  58. package/source/has-writable-keys.d.ts +23 -0
  59. package/source/if-any.d.ts +28 -0
  60. package/source/if-empty-object.d.ts +30 -0
  61. package/source/if-never.d.ts +28 -0
  62. package/source/if-null.d.ts +28 -0
  63. package/source/if-unknown.d.ts +28 -0
  64. package/source/if.d.ts +102 -0
  65. package/source/includes.d.ts +24 -0
  66. package/source/int-closed-range.d.ts +47 -0
  67. package/source/int-range.d.ts +69 -0
  68. package/source/internal/array.d.ts +151 -0
  69. package/source/internal/characters.d.ts +65 -0
  70. package/source/internal/enforce-optional.d.ts +49 -0
  71. package/source/internal/index.d.ts +11 -0
  72. package/source/internal/keys.d.ts +100 -0
  73. package/source/internal/numeric.d.ts +151 -0
  74. package/source/internal/object.d.ts +292 -0
  75. package/source/internal/string.d.ts +202 -0
  76. package/source/internal/tuple.d.ts +79 -0
  77. package/source/internal/type.d.ts +164 -0
  78. package/source/invariant-of.d.ts +85 -0
  79. package/source/is-any.d.ts +31 -0
  80. package/source/is-equal.d.ts +42 -0
  81. package/source/is-float.d.ts +43 -0
  82. package/source/is-integer.d.ts +60 -0
  83. package/source/is-literal.d.ts +315 -0
  84. package/source/is-lowercase.d.ts +38 -0
  85. package/source/is-never.d.ts +56 -0
  86. package/source/is-null.d.ts +22 -0
  87. package/source/is-nullable.d.ts +30 -0
  88. package/source/is-optional-key-of.d.ts +51 -0
  89. package/source/is-optional.d.ts +28 -0
  90. package/source/is-readonly-key-of.d.ts +55 -0
  91. package/source/is-required-key-of.d.ts +51 -0
  92. package/source/is-tuple.d.ts +92 -0
  93. package/source/is-undefined.d.ts +22 -0
  94. package/source/is-union.d.ts +40 -0
  95. package/source/is-unknown.d.ts +43 -0
  96. package/source/is-uppercase.d.ts +38 -0
  97. package/source/is-writable-key-of.d.ts +51 -0
  98. package/source/iterable-element.d.ts +66 -0
  99. package/source/join.d.ts +79 -0
  100. package/source/json-value.d.ts +33 -0
  101. package/source/jsonifiable.d.ts +37 -0
  102. package/source/jsonify.d.ts +127 -0
  103. package/source/kebab-case.d.ts +46 -0
  104. package/source/kebab-cased-properties-deep.d.ts +65 -0
  105. package/source/kebab-cased-properties.d.ts +42 -0
  106. package/source/key-as-string.d.ts +27 -0
  107. package/source/keys-of-union.d.ts +44 -0
  108. package/source/last-array-element.d.ts +36 -0
  109. package/source/less-than-or-equal.d.ts +24 -0
  110. package/source/less-than.d.ts +28 -0
  111. package/source/literal-to-primitive-deep.d.ts +71 -0
  112. package/source/literal-to-primitive.d.ts +38 -0
  113. package/source/literal-union.d.ts +39 -0
  114. package/source/merge-deep.d.ts +495 -0
  115. package/source/merge-exclusive.d.ts +45 -0
  116. package/source/merge.d.ts +62 -0
  117. package/source/multidimensional-array.d.ts +38 -0
  118. package/source/multidimensional-readonly-array.d.ts +38 -0
  119. package/source/non-empty-object.d.ts +38 -0
  120. package/source/non-empty-string.d.ts +32 -0
  121. package/source/non-empty-tuple.d.ts +24 -0
  122. package/source/numeric.d.ts +226 -0
  123. package/source/object-merge.d.ts +194 -0
  124. package/source/omit-deep.d.ts +154 -0
  125. package/source/omit-index-signature.d.ts +98 -0
  126. package/source/opaque.d.ts +3 -0
  127. package/source/optional-keys-of.d.ts +46 -0
  128. package/source/or.d.ts +89 -0
  129. package/source/override-properties.d.ts +43 -0
  130. package/source/package-json.d.ts +710 -0
  131. package/source/partial-deep.d.ts +155 -0
  132. package/source/partial-on-undefined-deep.d.ts +81 -0
  133. package/source/pascal-case.d.ts +51 -0
  134. package/source/pascal-cased-properties-deep.d.ts +72 -0
  135. package/source/pascal-cased-properties.d.ts +42 -0
  136. package/source/paths.d.ts +241 -0
  137. package/source/pick-deep.d.ts +152 -0
  138. package/source/pick-index-signature.d.ts +52 -0
  139. package/source/primitive.d.ts +15 -0
  140. package/source/promisable.d.ts +27 -0
  141. package/source/readonly-deep.d.ts +116 -0
  142. package/source/readonly-keys-of.d.ts +38 -0
  143. package/source/readonly-tuple.d.ts +34 -0
  144. package/source/remove-prefix.d.ts +132 -0
  145. package/source/replace.d.ts +87 -0
  146. package/source/require-all-or-none.d.ts +54 -0
  147. package/source/require-at-least-one.d.ts +50 -0
  148. package/source/require-exactly-one.d.ts +48 -0
  149. package/source/require-one-or-none.d.ts +49 -0
  150. package/source/required-deep.d.ts +76 -0
  151. package/source/required-keys-of.d.ts +38 -0
  152. package/source/schema.d.ts +119 -0
  153. package/source/screaming-snake-case.d.ts +30 -0
  154. package/source/set-field-type.d.ts +67 -0
  155. package/source/set-non-nullable-deep.d.ts +85 -0
  156. package/source/set-non-nullable.d.ts +41 -0
  157. package/source/set-optional.d.ts +46 -0
  158. package/source/set-parameter-type.d.ts +125 -0
  159. package/source/set-readonly.d.ts +44 -0
  160. package/source/set-required-deep.d.ts +64 -0
  161. package/source/set-required.d.ts +79 -0
  162. package/source/set-return-type.d.ts +31 -0
  163. package/source/shared-union-fields-deep.d.ts +180 -0
  164. package/source/shared-union-fields.d.ts +79 -0
  165. package/source/simplify-deep.d.ts +117 -0
  166. package/source/simplify.d.ts +61 -0
  167. package/source/single-key-object.d.ts +28 -0
  168. package/source/snake-case.d.ts +47 -0
  169. package/source/snake-cased-properties-deep.d.ts +65 -0
  170. package/source/snake-cased-properties.d.ts +42 -0
  171. package/source/split-on-rest-element.d.ts +106 -0
  172. package/source/split.d.ts +90 -0
  173. package/source/spread.d.ts +82 -0
  174. package/source/string-repeat.d.ts +49 -0
  175. package/source/string-slice.d.ts +39 -0
  176. package/source/stringified.d.ts +25 -0
  177. package/source/structured-cloneable.d.ts +89 -0
  178. package/source/subtract.d.ts +86 -0
  179. package/source/sum.d.ts +81 -0
  180. package/source/tagged-union.d.ts +53 -0
  181. package/source/tagged.d.ts +263 -0
  182. package/source/trim.d.ts +29 -0
  183. package/source/tsconfig-json.d.ts +1324 -0
  184. package/source/tuple-of.d.ts +80 -0
  185. package/source/tuple-to-object.d.ts +47 -0
  186. package/source/tuple-to-union.d.ts +54 -0
  187. package/source/typed-array.d.ts +19 -0
  188. package/source/undefined-on-partial-deep.d.ts +83 -0
  189. package/source/union-to-intersection.d.ts +35 -0
  190. package/source/union-to-tuple.d.ts +58 -0
  191. package/source/unknown-array.d.ts +27 -0
  192. package/source/unknown-map.d.ts +26 -0
  193. package/source/unknown-record.d.ts +33 -0
  194. package/source/unknown-set.d.ts +26 -0
  195. package/source/unwrap-partial.d.ts +33 -0
  196. package/source/value-of.d.ts +24 -0
  197. package/source/words.d.ts +122 -0
  198. package/source/writable-deep.d.ts +84 -0
  199. package/source/writable-keys-of.d.ts +34 -0
  200. package/source/writable.d.ts +72 -0
  201. package/source/xor.d.ts +83 -0
@@ -0,0 +1,292 @@
1
+ import type {Simplify} from '../simplify.d.ts';
2
+ import type {IsEqual} from '../is-equal.d.ts';
3
+ import type {KeysOfUnion} from '../keys-of-union.d.ts';
4
+ import type {RequiredKeysOf} from '../required-keys-of.d.ts';
5
+ import type {Merge} from '../merge.d.ts';
6
+ import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
7
+ import type {IsAny} from '../is-any.d.ts';
8
+ import type {If} from '../if.d.ts';
9
+ import type {IsNever} from '../is-never.d.ts';
10
+ import type {FilterDefinedKeys, FilterOptionalKeys} from './keys.d.ts';
11
+ import type {MapsSetsOrArrays, NonRecursiveType} from './type.d.ts';
12
+ import type {StringToNumber, ToString} from './string.d.ts';
13
+
14
+ /**
15
+ Create an object type with the given key `<Key>` and value `<Value>`.
16
+
17
+ It will copy the prefix and optional status of the same key from the given object `CopiedFrom` into the result.
18
+
19
+ @example
20
+ ```
21
+ type A = BuildObject<'a', string>;
22
+ //=> {a: string}
23
+
24
+ // Copy `readonly` and `?` from the key `a` of `{readonly a?: any}`
25
+ type B = BuildObject<'a', string, {readonly a?: any}>;
26
+ //=> {readonly a?: string}
27
+ ```
28
+ */
29
+ export type BuildObject<Key extends PropertyKey, Value, CopiedFrom extends object = {}> =
30
+ Key extends keyof CopiedFrom
31
+ ? Pick<{[_ in keyof CopiedFrom]: Value}, Key>
32
+ : Key extends `${infer NumberKey extends number}`
33
+ ? NumberKey extends keyof CopiedFrom
34
+ ? Pick<{[_ in keyof CopiedFrom]: Value}, NumberKey>
35
+ : {[_ in Key]: Value}
36
+ : {[_ in Key]: Value};
37
+
38
+ /**
39
+ Returns a boolean for whether the given type is a plain key-value object.
40
+ */
41
+ export type IsPlainObject<T> =
42
+ IsNever<T> extends true
43
+ ? false
44
+ : T extends NonRecursiveType | MapsSetsOrArrays
45
+ ? false
46
+ : T extends object
47
+ ? true
48
+ : false;
49
+
50
+ /**
51
+ Extract the object field type if T is an object and K is a key of T, return `never` otherwise.
52
+
53
+ It creates a type-safe way to access the member type of `unknown` type.
54
+ */
55
+ export type ObjectValue<T, K> =
56
+ K extends keyof T
57
+ ? T[K]
58
+ : ToString<K> extends keyof T
59
+ ? T[ToString<K>]
60
+ : K extends `${infer NumberK extends number}`
61
+ ? NumberK extends keyof T
62
+ ? T[NumberK]
63
+ : never
64
+ : never;
65
+
66
+ /**
67
+ For an object T, if it has any properties that are a union with `undefined`, make those into optional properties instead.
68
+
69
+ @example
70
+ ```
71
+ type User = {
72
+ firstName: string;
73
+ lastName: string | undefined;
74
+ };
75
+
76
+ type OptionalizedUser = UndefinedToOptional<User>;
77
+ //=> {
78
+ // firstName: string;
79
+ // lastName?: string;
80
+ // }
81
+ ```
82
+ */
83
+ export type UndefinedToOptional<T extends object> = Simplify<
84
+ {
85
+ // Property is not a union with `undefined`, keep it as-is.
86
+ [Key in keyof Pick<T, FilterDefinedKeys<T>>]: T[Key];
87
+ } & {
88
+ // Property _is_ a union with defined value. Set as optional (via `?`) and remove `undefined` from the union.
89
+ [Key in keyof Pick<T, FilterOptionalKeys<T>>]?: Exclude<T[Key], undefined>;
90
+ }
91
+ >;
92
+
93
+ /**
94
+ Works similar to the built-in `Pick` utility type, except for the following differences:
95
+ - Distributes over union types and allows picking keys from any member of the union type.
96
+ - Primitives types are returned as-is.
97
+ - Picks all keys if `Keys` is `any`.
98
+ - Doesn't pick `number` from a `string` index signature.
99
+
100
+ @example
101
+ ```
102
+ type ImageUpload = {
103
+ url: string;
104
+ size: number;
105
+ thumbnailUrl: string;
106
+ };
107
+
108
+ type VideoUpload = {
109
+ url: string;
110
+ duration: number;
111
+ encodingFormat: string;
112
+ };
113
+
114
+ // Distributes over union types and allows picking keys from any member of the union type
115
+ type MediaDisplay = HomomorphicPick<ImageUpload | VideoUpload, "url" | "size" | "duration">;
116
+ //=> {url: string; size: number} | {url: string; duration: number}
117
+
118
+ // Primitive types are returned as-is
119
+ type Primitive = HomomorphicPick<string | number, 'toUpperCase' | 'toString'>;
120
+ //=> string | number
121
+
122
+ // Picks all keys if `Keys` is `any`
123
+ type Any = HomomorphicPick<{a: 1; b: 2} | {c: 3}, any>;
124
+ //=> {a: 1; b: 2} | {c: 3}
125
+
126
+ // Doesn't pick `number` from a `string` index signature
127
+ type IndexSignature = HomomorphicPick<{[k: string]: unknown}, number>;
128
+ //=> {}
129
+ */
130
+ export type HomomorphicPick<T, Keys extends KeysOfUnion<T>> = {
131
+ [P in keyof T as Extract<P, Keys>]: T[P]
132
+ };
133
+
134
+ /**
135
+ Extract all possible values for a given key from a union of object types.
136
+
137
+ @example
138
+ ```
139
+ type Statuses = ValueOfUnion<{id: 1; status: 'open'} | {id: 2; status: 'closed'}, 'status'>;
140
+ //=> "open" | "closed"
141
+ ```
142
+ */
143
+ export type ValueOfUnion<Union, Key extends KeysOfUnion<Union>> =
144
+ Union extends unknown ? Key extends keyof Union ? Union[Key] : never : never;
145
+
146
+ /**
147
+ Extract all readonly keys from a union of object types.
148
+
149
+ @example
150
+ ```
151
+ type User = {
152
+ readonly id: string;
153
+ name: string;
154
+ };
155
+
156
+ type Post = {
157
+ readonly id: string;
158
+ readonly author: string;
159
+ body: string;
160
+ };
161
+
162
+ type ReadonlyKeys = ReadonlyKeysOfUnion<User | Post>;
163
+ //=> "id" | "author"
164
+ ```
165
+ */
166
+ export type ReadonlyKeysOfUnion<Union> = Union extends unknown ? keyof {
167
+ [Key in keyof Union as IsEqual<{[K in Key]: Union[Key]}, {readonly [K in Key]: Union[Key]}> extends true ? Key : never]: never
168
+ } : never;
169
+
170
+ /**
171
+ Merges user specified options with default options.
172
+
173
+ @example
174
+ ```
175
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
176
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
177
+ type SpecifiedOptions = {leavesOnly: true};
178
+
179
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
180
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
181
+ ```
182
+
183
+ @example
184
+ ```
185
+ // Complains if default values are not provided for optional options
186
+
187
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
188
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
189
+ type SpecifiedOptions = {};
190
+
191
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
192
+ // ~~~~~~~~~~~~~~~~~~~
193
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
194
+ ```
195
+
196
+ @example
197
+ ```
198
+ // Complains if an option's default type does not conform to the expected type
199
+
200
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
201
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
202
+ type SpecifiedOptions = {};
203
+
204
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
205
+ // ~~~~~~~~~~~~~~~~~~~
206
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
207
+ ```
208
+
209
+ @example
210
+ ```
211
+ // Complains if an option's specified type does not conform to the expected type
212
+
213
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
214
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
215
+ type SpecifiedOptions = {leavesOnly: 'yes'};
216
+
217
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
218
+ // ~~~~~~~~~~~~~~~~
219
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
220
+ ```
221
+ */
222
+ export type ApplyDefaultOptions<
223
+ Options extends object,
224
+ Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
225
+ SpecifiedOptions extends Options,
226
+ > =
227
+ If<IsAny<SpecifiedOptions>, Defaults,
228
+ If<IsNever<SpecifiedOptions>, Defaults,
229
+ Simplify<Merge<Defaults, {
230
+ [Key in keyof SpecifiedOptions
231
+ as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
232
+ ]: SpecifiedOptions[Key]
233
+ }> & Required<Options>>>>; // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
234
+
235
+ /**
236
+ Collapses literal types in a union into their corresponding primitive types, when possible. For example, `CollapseLiterals<'foo' | 'bar' | (string & {})>` returns `string`.
237
+
238
+ Note: This doesn't collapse literals within tagged types. For example, `CollapseLiterals<Tagged<'foo' | (string & {}), 'Tag'>>` returns `("foo" & Tag<"Tag", never>) | (string & Tag<"Tag", never>)` and not `string & Tag<"Tag", never>`.
239
+
240
+ Use-case: For collapsing unions created using {@link LiteralUnion}.
241
+
242
+ @example
243
+ ```
244
+ import type {LiteralUnion} from 'type-fest';
245
+
246
+ type A = CollapseLiterals<'foo' | 'bar' | (string & {})>;
247
+ //=> string
248
+
249
+ type B = CollapseLiterals<LiteralUnion<1 | 2 | 3, number>>;
250
+ //=> number
251
+
252
+ type C = CollapseLiterals<LiteralUnion<'onClick' | 'onChange', `on${string}`>>;
253
+ //=> `on${string}`
254
+
255
+ type D = CollapseLiterals<'click' | 'change' | (`on${string}` & {})>;
256
+ //=> 'click' | 'change' | `on${string}`
257
+
258
+ type E = CollapseLiterals<LiteralUnion<'foo' | 'bar', string> | null | undefined>;
259
+ //=> string | null | undefined
260
+ ```
261
+ */
262
+ export type CollapseLiterals<T> = {} extends T
263
+ ? T
264
+ : T extends infer U & {}
265
+ ? U
266
+ : T;
267
+
268
+ /**
269
+ Normalize keys by including string and number representations wherever applicable.
270
+
271
+ @example
272
+ ```ts
273
+ type A = NormalizedKeys<0 | '1'>;
274
+ //=> 0 | '0' | 1 | '1'
275
+
276
+ type B = NormalizedKeys<string>;
277
+ //=> string | number
278
+
279
+ type C = NormalizedKeys<number>;
280
+ //=> number | `${number}`
281
+
282
+ type D = NormalizedKeys<symbol | 'foo'>;
283
+ //=> symbol | 'foo'
284
+ ```
285
+ */
286
+ export type NormalizedKeys<Keys extends PropertyKey> =
287
+ | Keys
288
+ | (string extends Keys ? number : never)
289
+ | StringToNumber<Keys & string>
290
+ | ToString<Keys & number>;
291
+
292
+ export {};
@@ -0,0 +1,202 @@
1
+ import type {TupleOf} from '../tuple-of.d.ts';
2
+ import type {NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
3
+ import type {Trim} from '../trim.d.ts';
4
+ import type {Whitespace} from './characters.d.ts';
5
+
6
+ /**
7
+ Return a string representation of the given string or number.
8
+
9
+ Note: This type is not the return type of the `.toString()` function.
10
+ */
11
+ export type ToString<T> = T extends string | number ? `${T}` : never;
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
+ /**
50
+ Returns a boolean for whether the given string `S` starts with the given string `SearchString`.
51
+
52
+ @example
53
+ ```
54
+ type A = StartsWith<'abcde', 'abc'>;
55
+ //=> true
56
+
57
+ type B = StartsWith<'abcde', 'bc'>;
58
+ //=> false
59
+
60
+ type C = StartsWith<string, 'bc'>;
61
+ //=> never
62
+
63
+ type D = StartsWith<'abcde', string>;
64
+ //=> never
65
+ ```
66
+
67
+ @category String
68
+ @category Template literal
69
+ */
70
+ export type StartsWith<S extends string, SearchString extends string> = string extends S | SearchString
71
+ ? never
72
+ : S extends `${SearchString}${infer T}`
73
+ ? true
74
+ : false;
75
+
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
+ /**
116
+ Returns a boolean for whether a string is whitespace.
117
+ */
118
+ export type IsWhitespace<T extends string> = T extends Whitespace
119
+ ? true
120
+ : T extends `${Whitespace}${infer Rest}`
121
+ ? IsWhitespace<Rest>
122
+ : false;
123
+
124
+ /**
125
+ Returns a boolean for whether the string is numeric.
126
+
127
+ This type is a workaround for [Microsoft/TypeScript#46109](https://github.com/microsoft/TypeScript/issues/46109#issuecomment-930307987).
128
+ */
129
+ export type IsNumeric<T extends string> = T extends `${number}`
130
+ ? Trim<T> extends T
131
+ ? true
132
+ : false
133
+ : false;
134
+
135
+ /**
136
+ 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.
137
+
138
+ @example
139
+ ```
140
+ type A = SameLengthPositiveNumericStringGt<'50', '10'>;
141
+ //=> true
142
+
143
+ type B = SameLengthPositiveNumericStringGt<'10', '10'>;
144
+ //=> false
145
+ ```
146
+ */
147
+ type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}`
148
+ ? B extends `${infer FirstB}${infer RestB}`
149
+ ? FirstA extends FirstB
150
+ ? SameLengthPositiveNumericStringGt<RestA, RestB>
151
+ : PositiveNumericCharacterGt<FirstA, FirstB>
152
+ : never
153
+ : false;
154
+
155
+ type NumericString = '0123456789';
156
+
157
+ /**
158
+ Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
159
+
160
+ @example
161
+ ```
162
+ type A = PositiveNumericStringGt<'500', '1'>;
163
+ //=> true
164
+
165
+ type B = PositiveNumericStringGt<'1', '1'>;
166
+ //=> false
167
+
168
+ type C = PositiveNumericStringGt<'1', '500'>;
169
+ //=> false
170
+ ```
171
+ */
172
+ export type PositiveNumericStringGt<A extends string, B extends string> = A extends B
173
+ ? false
174
+ : [TupleOf<StringLength<A>, 0>, TupleOf<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]]
175
+ ? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
176
+ ? 0 extends Remain['length']
177
+ ? SameLengthPositiveNumericStringGt<A, B>
178
+ : true
179
+ : false
180
+ : never;
181
+
182
+ /**
183
+ Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
184
+
185
+ @example
186
+ ```
187
+ type A = PositiveNumericCharacterGt<'5', '1'>;
188
+ //=> true
189
+
190
+ type B = PositiveNumericCharacterGt<'1', '1'>;
191
+ //=> false
192
+ ```
193
+ */
194
+ type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
195
+ ? NumericString extends `${infer HeadB}${B}${infer TailB}`
196
+ ? HeadA extends `${HeadB}${infer _}${infer __}`
197
+ ? true
198
+ : false
199
+ : never
200
+ : never;
201
+
202
+ export {};
@@ -0,0 +1,79 @@
1
+ import type {GreaterThan} from '../greater-than.d.ts';
2
+ import type {LessThan} from '../less-than.d.ts';
3
+ import type {NegativeInfinity, PositiveInfinity} from '../numeric.d.ts';
4
+ import type {UnknownArray} from '../unknown-array.d.ts';
5
+
6
+ /**
7
+ Infer the length of the given tuple `<T>`.
8
+
9
+ Returns `never` if the given type is an non-fixed-length array like `Array<string>`.
10
+
11
+ @example
12
+ ```
13
+ type Tuple = TupleLength<[string, number, boolean]>;
14
+ //=> 3
15
+
16
+ type Array = TupleLength<string[]>;
17
+ //=> never
18
+
19
+ // Supports union types.
20
+ type Union = TupleLength<[] | [1, 2, 3] | number[]>;
21
+ //=> 1 | 3
22
+ ```
23
+ */
24
+ export type TupleLength<T extends UnknownArray> =
25
+ // `extends unknown` is used to convert `T` (if `T` is a union type) to
26
+ // a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types))
27
+ T extends unknown
28
+ ? number extends T['length']
29
+ ? never // Return never if the given type is an non-flexed-length array like `Array<string>`
30
+ : T['length']
31
+ : never; // Should never happen
32
+
33
+ /**
34
+ Returns the maximum value from a tuple of integers.
35
+
36
+ Note:
37
+ - Float numbers are not supported.
38
+
39
+ @example
40
+ ```
41
+ type A = TupleMax<[1, 2, 5, 3]>;
42
+ //=> 5
43
+
44
+ type B = TupleMax<[1, 2, 5, 3, 99, -1]>;
45
+ //=> 99
46
+ ```
47
+ */
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[]]
51
+ ? GreaterThan<F, Result> extends true
52
+ ? TupleMax<R, F>
53
+ : TupleMax<R, Result>
54
+ : Result;
55
+
56
+ /**
57
+ Returns the minimum value from a tuple of integers.
58
+
59
+ Note:
60
+ - Float numbers are not supported.
61
+
62
+ @example
63
+ ```
64
+ type A = TupleMin<[1, 2, 5, 3]>;
65
+ //=> 1
66
+
67
+ type B = TupleMin<[1, 2, 5, 3, -5]>;
68
+ //=> -5
69
+ ```
70
+ */
71
+ export type TupleMin<A extends number[], Result extends number = PositiveInfinity> = number extends A[number]
72
+ ? never
73
+ : A extends [infer F extends number, ...infer R extends number[]]
74
+ ? LessThan<F, Result> extends true
75
+ ? TupleMin<R, F>
76
+ : TupleMin<R, Result>
77
+ : Result;
78
+
79
+ export {};