@oscarpalmer/atoms 0.74.1 → 0.76.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 (282) hide show
  1. package/dist/js/array/chunk.cjs +18 -0
  2. package/dist/js/array/{chunk.mjs → chunk.js} +2 -2
  3. package/dist/js/array/compact.cjs +6 -0
  4. package/dist/js/array/{compact.mjs → compact.js} +0 -1
  5. package/dist/js/array/count.cjs +14 -0
  6. package/dist/js/array/count.js +14 -0
  7. package/dist/js/array/exists.cjs +14 -0
  8. package/dist/js/array/exists.js +14 -0
  9. package/dist/js/array/filter.cjs +14 -0
  10. package/dist/js/array/filter.js +14 -0
  11. package/dist/js/array/find.cjs +14 -0
  12. package/dist/js/array/find.js +14 -0
  13. package/dist/js/array/group-by.cjs +35 -0
  14. package/dist/js/array/group-by.js +35 -0
  15. package/dist/js/array/index-of.cjs +14 -0
  16. package/dist/js/array/index-of.js +14 -0
  17. package/dist/js/array/index.cjs +40 -0
  18. package/dist/js/array/index.js +34 -381
  19. package/dist/js/array/insert.cjs +31 -0
  20. package/dist/js/array/insert.js +31 -0
  21. package/dist/js/array/models.cjs +1 -0
  22. package/dist/js/array/models.js +1 -0
  23. package/dist/js/array/shuffle.cjs +13 -0
  24. package/dist/js/array/{shuffle.mjs → shuffle.js} +2 -3
  25. package/dist/js/array/sort.cjs +53 -0
  26. package/dist/js/array/{sort.mjs → sort.js} +13 -9
  27. package/dist/js/array/splice.cjs +14 -0
  28. package/dist/js/array/splice.js +14 -0
  29. package/dist/js/array/to-map.cjs +27 -0
  30. package/dist/js/array/to-map.js +27 -0
  31. package/dist/js/array/to-record.cjs +12 -0
  32. package/dist/js/array/to-record.js +12 -0
  33. package/dist/js/array/unique.cjs +7 -0
  34. package/dist/js/array/unique.js +7 -0
  35. package/dist/js/colour/base.cjs +18 -0
  36. package/dist/js/colour/{base.mjs → base.js} +4 -3
  37. package/dist/js/colour/functions.cjs +96 -0
  38. package/dist/js/colour/{functions.mjs → functions.js} +18 -19
  39. package/dist/js/colour/hex.cjs +56 -0
  40. package/dist/js/colour/{hex.mjs → hex.js} +34 -15
  41. package/dist/js/colour/hsl.cjs +85 -0
  42. package/dist/js/colour/{hsl.mjs → hsl.js} +40 -12
  43. package/dist/js/colour/index.cjs +29 -0
  44. package/dist/js/colour/index.js +14 -272
  45. package/dist/js/colour/is.cjs +27 -0
  46. package/dist/js/colour/{is.mjs → is.js} +7 -6
  47. package/dist/js/colour/rgb.cjs +87 -0
  48. package/dist/js/colour/{rgb.mjs → rgb.js} +43 -12
  49. package/dist/js/{emitter.mjs → emitter.cjs} +88 -56
  50. package/dist/js/emitter.js +85 -56
  51. package/dist/js/{function.mjs → function.cjs} +66 -46
  52. package/dist/js/function.js +61 -48
  53. package/dist/js/index.cjs +136 -0
  54. package/dist/js/index.js +133 -1724
  55. package/dist/js/internal/array/callbacks.cjs +23 -0
  56. package/dist/js/internal/array/callbacks.js +23 -0
  57. package/dist/js/internal/array/find.cjs +51 -0
  58. package/dist/js/internal/array/find.js +51 -0
  59. package/dist/js/internal/value/handle.cjs +21 -0
  60. package/dist/js/internal/{value-handle.mjs → value/handle.js} +0 -1
  61. package/dist/js/{is.mjs → is.cjs} +17 -18
  62. package/dist/js/is.js +15 -27
  63. package/dist/js/logger.cjs +108 -0
  64. package/dist/js/logger.js +44 -8
  65. package/dist/js/{math.mjs → math.cjs} +7 -8
  66. package/dist/js/math.js +4 -5
  67. package/dist/js/models.cjs +1 -0
  68. package/dist/js/models.js +1 -0
  69. package/dist/js/{number.mjs → number.cjs} +8 -8
  70. package/dist/js/number.js +5 -5
  71. package/dist/js/{query.mjs → query.cjs} +18 -18
  72. package/dist/js/query.js +17 -86
  73. package/dist/js/{queue.mjs → queue.cjs} +12 -13
  74. package/dist/js/queue.js +9 -10
  75. package/dist/js/{random.mjs → random.cjs} +14 -21
  76. package/dist/js/random.js +9 -26
  77. package/dist/js/{sized.mjs → sized.cjs} +68 -33
  78. package/dist/js/sized.js +66 -38
  79. package/dist/js/string/case.cjs +41 -0
  80. package/dist/js/string/{case.mjs → case.js} +13 -9
  81. package/dist/js/string/index.cjs +59 -0
  82. package/dist/js/string/index.js +30 -100
  83. package/dist/js/string/template.cjs +21 -0
  84. package/dist/js/string/{template.mjs → template.js} +4 -5
  85. package/dist/js/{touch.mjs → touch.cjs} +4 -7
  86. package/dist/js/touch.js +3 -5
  87. package/dist/js/value/clone.cjs +76 -0
  88. package/dist/js/value/{clone.mjs → clone.js} +5 -6
  89. package/dist/js/value/compare.cjs +59 -0
  90. package/dist/js/value/{compare.mjs → compare.js} +4 -6
  91. package/dist/js/value/diff.cjs +75 -0
  92. package/dist/js/value/{diff.mjs → diff.js} +13 -11
  93. package/dist/js/value/equal.cjs +106 -0
  94. package/dist/js/value/{equal.mjs → equal.js} +13 -8
  95. package/dist/js/value/get.cjs +21 -0
  96. package/dist/js/value/{get.mjs → get.js} +9 -4
  97. package/dist/js/value/index.cjs +30 -0
  98. package/dist/js/value/index.js +19 -510
  99. package/dist/js/value/merge.cjs +35 -0
  100. package/dist/js/value/{merge.mjs → merge.js} +7 -6
  101. package/dist/js/value/set.cjs +25 -0
  102. package/dist/js/value/{set.mjs → set.js} +3 -4
  103. package/dist/js/value/smush.cjs +26 -0
  104. package/dist/js/value/{smush.mjs → smush.js} +3 -4
  105. package/dist/js/value/unsmush.cjs +39 -0
  106. package/dist/js/value/{unsmush.mjs → unsmush.js} +10 -7
  107. package/package.json +50 -47
  108. package/src/js/array/chunk.ts +6 -5
  109. package/src/js/array/compact.ts +8 -10
  110. package/src/js/array/count.ts +39 -19
  111. package/src/js/array/exists.ts +39 -15
  112. package/src/js/array/filter.ts +42 -20
  113. package/src/js/array/find.ts +45 -20
  114. package/src/js/array/group-by.ts +158 -27
  115. package/src/js/array/index-of.ts +40 -18
  116. package/src/js/array/index.ts +10 -9
  117. package/src/js/array/insert.ts +28 -14
  118. package/src/js/array/models.ts +18 -15
  119. package/src/js/array/shuffle.ts +3 -3
  120. package/src/js/array/sort.ts +32 -34
  121. package/src/js/array/splice.ts +22 -26
  122. package/src/js/array/to-map.ts +133 -45
  123. package/src/js/array/to-record.ts +137 -39
  124. package/src/js/array/unique.ts +20 -15
  125. package/src/js/colour/base.ts +3 -3
  126. package/src/js/colour/functions.ts +4 -6
  127. package/src/js/colour/hex.ts +5 -5
  128. package/src/js/colour/hsl.ts +18 -12
  129. package/src/js/colour/index.ts +1 -1
  130. package/src/js/colour/is.ts +3 -3
  131. package/src/js/colour/rgb.ts +17 -11
  132. package/src/js/emitter.ts +12 -9
  133. package/src/js/function.ts +17 -17
  134. package/src/js/index.ts +16 -16
  135. package/src/js/internal/array/callbacks.ts +33 -0
  136. package/src/js/internal/{array-find.ts → array/find.ts} +23 -24
  137. package/src/js/internal/{value-handle.ts → value/handle.ts} +1 -1
  138. package/src/js/is.ts +2 -2
  139. package/src/js/logger.ts +14 -14
  140. package/src/js/math.ts +1 -1
  141. package/src/js/models.ts +5 -5
  142. package/src/js/number.ts +2 -2
  143. package/src/js/query.ts +6 -10
  144. package/src/js/queue.ts +1 -1
  145. package/src/js/random.ts +9 -19
  146. package/src/js/sized.ts +145 -145
  147. package/src/js/string/case.ts +1 -1
  148. package/src/js/string/index.ts +13 -7
  149. package/src/js/string/template.ts +6 -3
  150. package/src/js/value/clone.ts +3 -7
  151. package/src/js/value/compare.ts +3 -5
  152. package/src/js/value/diff.ts +4 -4
  153. package/src/js/value/equal.ts +2 -2
  154. package/src/js/value/get.ts +3 -3
  155. package/src/js/value/index.ts +2 -2
  156. package/src/js/value/merge.ts +3 -3
  157. package/src/js/value/set.ts +3 -4
  158. package/src/js/value/smush.ts +4 -4
  159. package/src/js/value/unsmush.ts +4 -4
  160. package/types/array/chunk.d.cts +8 -0
  161. package/types/array/chunk.d.ts +2 -2
  162. package/types/array/compact.d.cts +12 -0
  163. package/types/array/compact.d.ts +4 -4
  164. package/types/array/count.d.cts +24 -0
  165. package/types/array/count.d.ts +13 -7
  166. package/types/array/exists.d.cts +26 -0
  167. package/types/array/exists.d.ts +11 -3
  168. package/types/array/filter.d.cts +26 -0
  169. package/types/array/filter.d.ts +14 -6
  170. package/types/array/find.d.cts +26 -0
  171. package/types/array/find.d.ts +14 -6
  172. package/types/array/group-by.d.cts +58 -0
  173. package/types/array/group-by.d.ts +49 -5
  174. package/types/array/index-of.d.cts +26 -0
  175. package/types/array/index-of.d.ts +14 -6
  176. package/types/array/index.d.cts +239 -95
  177. package/types/array/index.d.ts +7 -6
  178. package/types/array/insert.d.cts +16 -0
  179. package/types/array/insert.d.ts +10 -5
  180. package/types/array/models.d.cts +14 -11
  181. package/types/array/models.d.ts +14 -12
  182. package/types/array/shuffle.d.cts +8 -0
  183. package/types/array/shuffle.d.ts +2 -2
  184. package/types/array/sort.d.cts +24 -0
  185. package/types/array/sort.d.ts +10 -10
  186. package/types/array/splice.d.cts +22 -0
  187. package/types/array/splice.d.ts +4 -4
  188. package/types/array/to-map.d.cts +61 -0
  189. package/types/array/to-map.d.ts +42 -20
  190. package/types/array/to-record.d.cts +61 -0
  191. package/types/array/to-record.d.ts +42 -20
  192. package/types/array/unique.d.cts +21 -0
  193. package/types/array/unique.d.ts +10 -6
  194. package/types/colour/base.d.cts +153 -0
  195. package/types/colour/base.d.ts +2 -2
  196. package/types/colour/functions.d.cts +173 -0
  197. package/types/colour/functions.d.ts +3 -3
  198. package/types/colour/hex.d.cts +157 -0
  199. package/types/colour/hex.d.ts +4 -4
  200. package/types/colour/hsl.d.cts +157 -0
  201. package/types/colour/hsl.d.ts +16 -10
  202. package/types/colour/index.d.cts +28 -16
  203. package/types/colour/index.d.ts +1 -1
  204. package/types/colour/is.d.cts +170 -0
  205. package/types/colour/is.d.ts +3 -3
  206. package/types/colour/rgb.d.cts +157 -0
  207. package/types/colour/rgb.d.ts +15 -9
  208. package/types/emitter.d.cts +11 -8
  209. package/types/emitter.d.ts +11 -8
  210. package/types/function.d.cts +10 -10
  211. package/types/function.d.ts +11 -11
  212. package/types/index.d.cts +1120 -736
  213. package/types/index.d.ts +16 -16
  214. package/types/internal/array/callbacks.d.cts +11 -0
  215. package/types/internal/array/callbacks.d.ts +2 -0
  216. package/types/internal/array/find.d.cts +7 -0
  217. package/types/internal/array/find.d.ts +3 -0
  218. package/types/internal/value/handle.d.cts +37 -0
  219. package/types/internal/{value-handle.d.ts → value/handle.d.ts} +1 -1
  220. package/types/is.d.ts +1 -1
  221. package/types/logger.d.cts +13 -21
  222. package/types/logger.d.ts +13 -21
  223. package/types/math.d.cts +1 -1
  224. package/types/math.d.ts +1 -1
  225. package/types/models.d.cts +4 -4
  226. package/types/models.d.ts +1 -4
  227. package/types/number.d.cts +2 -2
  228. package/types/number.d.ts +2 -2
  229. package/types/query.d.cts +2 -2
  230. package/types/query.d.ts +3 -3
  231. package/types/queue.d.cts +1 -1
  232. package/types/queue.d.ts +1 -1
  233. package/types/random.d.cts +8 -12
  234. package/types/random.d.ts +8 -12
  235. package/types/sized.d.cts +6 -6
  236. package/types/sized.d.ts +6 -6
  237. package/types/string/case.d.cts +28 -0
  238. package/types/string/index.d.cts +30 -27
  239. package/types/string/index.d.ts +3 -3
  240. package/types/string/template.d.cts +50 -0
  241. package/types/string/template.d.ts +4 -1
  242. package/types/value/clone.d.cts +8 -0
  243. package/types/value/clone.d.ts +4 -2
  244. package/types/value/compare.d.cts +8 -0
  245. package/types/value/diff.d.cts +25 -0
  246. package/types/value/equal.d.cts +12 -0
  247. package/types/value/get.d.cts +1319 -0
  248. package/types/value/get.d.ts +1 -1
  249. package/types/value/index.d.cts +8 -6
  250. package/types/value/index.d.ts +2 -2
  251. package/types/value/merge.d.cts +72 -0
  252. package/types/value/merge.d.ts +2 -2
  253. package/types/value/set.d.cts +1011 -0
  254. package/types/value/set.d.ts +1 -1
  255. package/types/value/smush.d.cts +1368 -0
  256. package/types/value/smush.d.ts +2 -2
  257. package/types/value/unsmush.d.cts +141 -0
  258. package/types/value/unsmush.d.ts +2 -2
  259. package/dist/js/array/count.mjs +0 -8
  260. package/dist/js/array/exists.mjs +0 -8
  261. package/dist/js/array/filter.mjs +0 -8
  262. package/dist/js/array/find.mjs +0 -8
  263. package/dist/js/array/group-by.mjs +0 -33
  264. package/dist/js/array/index-of.mjs +0 -8
  265. package/dist/js/array/index.mjs +0 -34
  266. package/dist/js/array/insert.mjs +0 -22
  267. package/dist/js/array/models.mjs +0 -0
  268. package/dist/js/array/splice.mjs +0 -9
  269. package/dist/js/array/to-map.mjs +0 -27
  270. package/dist/js/array/to-record.mjs +0 -8
  271. package/dist/js/array/unique.mjs +0 -8
  272. package/dist/js/colour/index.mjs +0 -30
  273. package/dist/js/index.mjs +0 -17
  274. package/dist/js/internal/array-callbacks.mjs +0 -19
  275. package/dist/js/internal/array-find.mjs +0 -51
  276. package/dist/js/logger.mjs +0 -69
  277. package/dist/js/models.mjs +0 -0
  278. package/dist/js/string/index.mjs +0 -44
  279. package/dist/js/value/index.mjs +0 -23
  280. package/src/js/internal/array-callbacks.ts +0 -28
  281. package/types/internal/array-callbacks.d.ts +0 -2
  282. package/types/internal/array-find.d.ts +0 -4
@@ -0,0 +1,1319 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
5
+
6
+ @category Type
7
+ */
8
+ export type Primitive =
9
+ | null
10
+ | undefined
11
+ | string
12
+ | number
13
+ | boolean
14
+ | symbol
15
+ | bigint;
16
+ declare const emptyObjectSymbol: unique symbol;
17
+ /**
18
+ Represents a strictly empty plain object, the `{}` value.
19
+
20
+ When you annotate something as the type `{}`, it can be anything except `null` and `undefined`. This means that you cannot use `{}` to represent an empty plain object ([read more](https://stackoverflow.com/questions/47339869/typescript-empty-object-and-any-difference/52193484#52193484)).
21
+
22
+ @example
23
+ ```
24
+ import type {EmptyObject} from 'type-fest';
25
+
26
+ // The following illustrates the problem with `{}`.
27
+ const foo1: {} = {}; // Pass
28
+ const foo2: {} = []; // Pass
29
+ const foo3: {} = 42; // Pass
30
+ const foo4: {} = {a: 1}; // Pass
31
+
32
+ // With `EmptyObject` only the first case is valid.
33
+ const bar1: EmptyObject = {}; // Pass
34
+ const bar2: EmptyObject = 42; // Fail
35
+ const bar3: EmptyObject = []; // Fail
36
+ const bar4: EmptyObject = {a: 1}; // Fail
37
+ ```
38
+
39
+ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<never, never>` do not work. See {@link https://github.com/sindresorhus/type-fest/issues/395 #395}.
40
+
41
+ @category Object
42
+ */
43
+ export type EmptyObject = {
44
+ [emptyObjectSymbol]?: never;
45
+ };
46
+ /**
47
+ Returns a boolean for whether the two given types are equal.
48
+
49
+ @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
50
+ @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
51
+
52
+ Use-cases:
53
+ - If you want to make a conditional branch based on the result of a comparison of two types.
54
+
55
+ @example
56
+ ```
57
+ import type {IsEqual} from 'type-fest';
58
+
59
+ // This type returns a boolean for whether the given array includes the given item.
60
+ // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
61
+ type Includes<Value extends readonly any[], Item> =
62
+ Value extends readonly [Value[0], ...infer rest]
63
+ ? IsEqual<Value[0], Item> extends true
64
+ ? true
65
+ : Includes<rest, Item>
66
+ : false;
67
+ ```
68
+
69
+ @category Type Guard
70
+ @category Utilities
71
+ */
72
+ export type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <
73
+ G,
74
+ >() => G extends B ? 1 : 2
75
+ ? true
76
+ : false;
77
+ /**
78
+ Represents an object with `unknown` value. You probably want this instead of `{}`.
79
+
80
+ Use case: You have an object whose keys and values are unknown to you.
81
+
82
+ @example
83
+ ```
84
+ import type {UnknownRecord} from 'type-fest';
85
+
86
+ function toJson(object: UnknownRecord) {
87
+ return JSON.stringify(object);
88
+ }
89
+
90
+ toJson({hello: 'world'});
91
+ //=> '{"hello":"world"}'
92
+
93
+ function isObject(value: unknown): value is UnknownRecord {
94
+ return typeof value === 'object' && value !== null;
95
+ }
96
+
97
+ isObject({hello: 'world'});
98
+ //=> true
99
+
100
+ isObject('hello');
101
+ //=> false
102
+ ```
103
+
104
+ @category Type
105
+ @category Object
106
+ */
107
+ export type UnknownRecord = Record<PropertyKey, unknown>;
108
+ /**
109
+ Represents an array with `unknown` value.
110
+
111
+ Use case: You want a type that all arrays can be assigned to, but you don't care about the value.
112
+
113
+ @example
114
+ ```
115
+ import type {UnknownArray} from 'type-fest';
116
+
117
+ type IsArray<T> = T extends UnknownArray ? true : false;
118
+
119
+ type A = IsArray<['foo']>;
120
+ //=> true
121
+
122
+ type B = IsArray<readonly number[]>;
123
+ //=> true
124
+
125
+ type C = IsArray<string>;
126
+ //=> false
127
+ ```
128
+
129
+ @category Type
130
+ @category Array
131
+ */
132
+ export type UnknownArray = readonly unknown[];
133
+ /**
134
+ Returns the static, fixed-length portion of the given array, excluding variable-length parts.
135
+
136
+ @example
137
+ ```
138
+ type A = [string, number, boolean, ...string[]];
139
+ type B = StaticPartOfArray<A>;
140
+ //=> [string, number, boolean]
141
+ ```
142
+ */
143
+ export type StaticPartOfArray<
144
+ T extends UnknownArray,
145
+ Result extends UnknownArray = [],
146
+ > = T extends unknown
147
+ ? number extends T['length']
148
+ ? T extends readonly [infer U, ...infer V]
149
+ ? StaticPartOfArray<V, [...Result, U]>
150
+ : Result
151
+ : T
152
+ : never; // Should never happen
153
+ /**
154
+ Returns the variable, non-fixed-length portion of the given array, excluding static-length parts.
155
+
156
+ @example
157
+ ```
158
+ type A = [string, number, boolean, ...string[]];
159
+ type B = VariablePartOfArray<A>;
160
+ //=> string[]
161
+ ```
162
+ */
163
+ export type VariablePartOfArray<T extends UnknownArray> = T extends unknown
164
+ ? T extends readonly [...StaticPartOfArray<T>, ...infer U]
165
+ ? U
166
+ : []
167
+ : never; // Should never happen
168
+ export type StringDigit =
169
+ | '0'
170
+ | '1'
171
+ | '2'
172
+ | '3'
173
+ | '4'
174
+ | '5'
175
+ | '6'
176
+ | '7'
177
+ | '8'
178
+ | '9';
179
+ /**
180
+ Returns a boolean for whether the given type is `any`.
181
+
182
+ @link https://stackoverflow.com/a/49928360/1490091
183
+
184
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
185
+
186
+ @example
187
+ ```
188
+ import type {IsAny} from 'type-fest';
189
+
190
+ const typedObject = {a: 1, b: 2} as const;
191
+ const anyObject: any = {a: 1, b: 2};
192
+
193
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
194
+ return obj[key];
195
+ }
196
+
197
+ const typedA = get(typedObject, 'a');
198
+ //=> 1
199
+
200
+ const anyA = get(anyObject, 'a');
201
+ //=> any
202
+ ```
203
+
204
+ @category Type Guard
205
+ @category Utilities
206
+ */
207
+ export type IsAny<T> = 0 extends 1 & T ? true : false;
208
+ export type Numeric = number | bigint;
209
+ export type Zero = 0 | 0n;
210
+ /**
211
+ Matches the hidden `Infinity` type.
212
+
213
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
214
+
215
+ @see NegativeInfinity
216
+
217
+ @category Numeric
218
+ */
219
+ // See https://github.com/microsoft/TypeScript/issues/31752
220
+ // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
221
+ export type PositiveInfinity = 1e999;
222
+ /**
223
+ Matches the hidden `-Infinity` type.
224
+
225
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
226
+
227
+ @see PositiveInfinity
228
+
229
+ @category Numeric
230
+ */
231
+ // See https://github.com/microsoft/TypeScript/issues/31752
232
+ // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
233
+ export type NegativeInfinity = -1e999;
234
+ /**
235
+ A negative `number`/`bigint` (`-∞ < x < 0`)
236
+
237
+ Use-case: Validating and documenting parameters.
238
+
239
+ @see NegativeInteger
240
+ @see NonNegative
241
+
242
+ @category Numeric
243
+ */
244
+ export type Negative<T extends Numeric> = T extends Zero
245
+ ? never
246
+ : `${T}` extends `-${string}`
247
+ ? T
248
+ : never;
249
+ /**
250
+ Returns a boolean for whether the given number is a negative number.
251
+
252
+ @see Negative
253
+
254
+ @example
255
+ ```
256
+ import type {IsNegative} from 'type-fest';
257
+
258
+ type ShouldBeFalse = IsNegative<1>;
259
+ type ShouldBeTrue = IsNegative<-1>;
260
+ ```
261
+
262
+ @category Numeric
263
+ */
264
+ export type IsNegative<T extends Numeric> = T extends Negative<T>
265
+ ? true
266
+ : false;
267
+ /**
268
+ Returns a boolean for whether two given types are both true.
269
+
270
+ Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
271
+
272
+ @example
273
+ ```
274
+ import type {And} from 'type-fest';
275
+
276
+ And<true, true>;
277
+ //=> true
278
+
279
+ And<true, false>;
280
+ //=> false
281
+ ```
282
+
283
+ @see {@link Or}
284
+ */
285
+ export type And<A extends boolean, B extends boolean> = [
286
+ A,
287
+ B,
288
+ ][number] extends true
289
+ ? true
290
+ : true extends [IsEqual<A, false>, IsEqual<B, false>][number]
291
+ ? false
292
+ : never;
293
+ /**
294
+ Returns a boolean for whether either of two given types are true.
295
+
296
+ Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
297
+
298
+ @example
299
+ ```
300
+ import type {Or} from 'type-fest';
301
+
302
+ Or<true, false>;
303
+ //=> true
304
+
305
+ Or<false, false>;
306
+ //=> false
307
+ ```
308
+
309
+ @see {@link And}
310
+ */
311
+ export type Or<A extends boolean, B extends boolean> = [
312
+ A,
313
+ B,
314
+ ][number] extends false
315
+ ? false
316
+ : true extends [IsEqual<A, true>, IsEqual<B, true>][number]
317
+ ? true
318
+ : never;
319
+ /**
320
+ Returns a boolean for whether a given number is greater than another number.
321
+
322
+ @example
323
+ ```
324
+ import type {GreaterThan} from 'type-fest';
325
+
326
+ GreaterThan<1, -5>;
327
+ //=> true
328
+
329
+ GreaterThan<1, 1>;
330
+ //=> false
331
+
332
+ GreaterThan<1, 5>;
333
+ //=> false
334
+ ```
335
+ */
336
+ export type GreaterThan<A extends number, B extends number> = number extends
337
+ | A
338
+ | B
339
+ ? never
340
+ : [
341
+ IsEqual<A, PositiveInfinity>,
342
+ IsEqual<A, NegativeInfinity>,
343
+ IsEqual<B, PositiveInfinity>,
344
+ IsEqual<B, NegativeInfinity>,
345
+ ] extends infer R extends [boolean, boolean, boolean, boolean]
346
+ ? Or<
347
+ And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
348
+ And<IsEqual<R[3], true>, IsEqual<R[1], false>>
349
+ > extends true
350
+ ? true
351
+ : Or<
352
+ And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
353
+ And<IsEqual<R[2], true>, IsEqual<R[0], false>>
354
+ > extends true
355
+ ? false
356
+ : true extends R[number]
357
+ ? false
358
+ : [IsNegative<A>, IsNegative<B>] extends infer R extends [
359
+ boolean,
360
+ boolean,
361
+ ]
362
+ ? [true, false] extends R
363
+ ? false
364
+ : [false, true] extends R
365
+ ? true
366
+ : [false, false] extends R
367
+ ? PositiveNumericStringGt<`${A}`, `${B}`>
368
+ : PositiveNumericStringGt<
369
+ `${NumberAbsolute<B>}`,
370
+ `${NumberAbsolute<A>}`
371
+ >
372
+ : never
373
+ : never;
374
+ /**
375
+ Returns a boolean for whether a given number is greater than or equal to another number.
376
+
377
+ @example
378
+ ```
379
+ import type {GreaterThanOrEqual} from 'type-fest';
380
+
381
+ GreaterThanOrEqual<1, -5>;
382
+ //=> true
383
+
384
+ GreaterThanOrEqual<1, 1>;
385
+ //=> true
386
+
387
+ GreaterThanOrEqual<1, 5>;
388
+ //=> false
389
+ ```
390
+ */
391
+ export type GreaterThanOrEqual<
392
+ A extends number,
393
+ B extends number,
394
+ > = number extends A | B ? never : A extends B ? true : GreaterThan<A, B>;
395
+ /**
396
+ Returns a boolean for whether a given number is less than another number.
397
+
398
+ @example
399
+ ```
400
+ import type {LessThan} from 'type-fest';
401
+
402
+ LessThan<1, -5>;
403
+ //=> false
404
+
405
+ LessThan<1, 1>;
406
+ //=> false
407
+
408
+ LessThan<1, 5>;
409
+ //=> true
410
+ ```
411
+ */
412
+ export type LessThan<A extends number, B extends number> = number extends A | B
413
+ ? never
414
+ : GreaterThanOrEqual<A, B> extends true
415
+ ? false
416
+ : true;
417
+ /**
418
+ Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`.
419
+
420
+ If `<Fill>` is not provided, it will default to `unknown`.
421
+
422
+ @link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
423
+ */
424
+ export type BuildTuple<
425
+ L extends number,
426
+ Fill = unknown,
427
+ T extends readonly unknown[] = [],
428
+ > = T['length'] extends L ? T : BuildTuple<L, Fill, [...T, Fill]>;
429
+ /**
430
+ Returns the maximum value from a tuple of integers.
431
+
432
+ Note:
433
+ - Float numbers are not supported.
434
+
435
+ @example
436
+ ```
437
+ ArrayMax<[1, 2, 5, 3]>;
438
+ //=> 5
439
+
440
+ ArrayMax<[1, 2, 5, 3, 99, -1]>;
441
+ //=> 99
442
+ ```
443
+ */
444
+ export type TupleMax<
445
+ A extends number[],
446
+ Result extends number = NegativeInfinity,
447
+ > = number extends A[number]
448
+ ? never
449
+ : A extends [infer F extends number, ...infer R extends number[]]
450
+ ? GreaterThan<F, Result> extends true
451
+ ? TupleMax<R, F>
452
+ : TupleMax<R, Result>
453
+ : Result;
454
+ /**
455
+ Returns the minimum value from a tuple of integers.
456
+
457
+ Note:
458
+ - Float numbers are not supported.
459
+
460
+ @example
461
+ ```
462
+ ArrayMin<[1, 2, 5, 3]>;
463
+ //=> 1
464
+
465
+ ArrayMin<[1, 2, 5, 3, -5]>;
466
+ //=> -5
467
+ ```
468
+ */
469
+ export type TupleMin<
470
+ A extends number[],
471
+ Result extends number = PositiveInfinity,
472
+ > = number extends A[number]
473
+ ? never
474
+ : A extends [infer F extends number, ...infer R extends number[]]
475
+ ? LessThan<F, Result> extends true
476
+ ? TupleMin<R, F>
477
+ : TupleMin<R, Result>
478
+ : Result;
479
+ /**
480
+ Return a string representation of the given string or number.
481
+
482
+ Note: This type is not the return type of the `.toString()` function.
483
+ */
484
+ export type ToString<T> = T extends string | number ? `${T}` : never;
485
+ /**
486
+ Converts a numeric string to a number.
487
+
488
+ @example
489
+ ```
490
+ type PositiveInt = StringToNumber<'1234'>;
491
+ //=> 1234
492
+
493
+ type NegativeInt = StringToNumber<'-1234'>;
494
+ //=> -1234
495
+
496
+ type PositiveFloat = StringToNumber<'1234.56'>;
497
+ //=> 1234.56
498
+
499
+ type NegativeFloat = StringToNumber<'-1234.56'>;
500
+ //=> -1234.56
501
+
502
+ type PositiveInfinity = StringToNumber<'Infinity'>;
503
+ //=> Infinity
504
+
505
+ type NegativeInfinity = StringToNumber<'-Infinity'>;
506
+ //=> -Infinity
507
+ ```
508
+
509
+ @category String
510
+ @category Numeric
511
+ @category Template literal
512
+ */
513
+ export type StringToNumber<S extends string> =
514
+ S extends `${infer N extends number}`
515
+ ? N
516
+ : S extends 'Infinity'
517
+ ? PositiveInfinity
518
+ : S extends '-Infinity'
519
+ ? NegativeInfinity
520
+ : never;
521
+ /**
522
+ Returns an array of the characters of the string.
523
+
524
+ @example
525
+ ```
526
+ StringToArray<'abcde'>;
527
+ //=> ['a', 'b', 'c', 'd', 'e']
528
+
529
+ StringToArray<string>;
530
+ //=> never
531
+ ```
532
+
533
+ @category String
534
+ */
535
+ export type StringToArray<
536
+ S extends string,
537
+ Result extends string[] = [],
538
+ > = string extends S
539
+ ? never
540
+ : S extends `${infer F}${infer R}`
541
+ ? StringToArray<R, [...Result, F]>
542
+ : Result;
543
+ /**
544
+ Returns the length of the given string.
545
+
546
+ @example
547
+ ```
548
+ StringLength<'abcde'>;
549
+ //=> 5
550
+
551
+ StringLength<string>;
552
+ //=> never
553
+ ```
554
+
555
+ @category String
556
+ @category Template literal
557
+ */
558
+ export type StringLength<S extends string> = string extends S
559
+ ? never
560
+ : StringToArray<S>['length'];
561
+ /**
562
+ 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.
563
+
564
+ @example
565
+ ```
566
+ SameLengthPositiveNumericStringGt<'50', '10'>;
567
+ //=> true
568
+
569
+ SameLengthPositiveNumericStringGt<'10', '10'>;
570
+ //=> false
571
+ ```
572
+ */
573
+ export type SameLengthPositiveNumericStringGt<
574
+ A extends string,
575
+ B extends string,
576
+ > = A extends `${infer FirstA}${infer RestA}`
577
+ ? B extends `${infer FirstB}${infer RestB}`
578
+ ? FirstA extends FirstB
579
+ ? SameLengthPositiveNumericStringGt<RestA, RestB>
580
+ : PositiveNumericCharacterGt<FirstA, FirstB>
581
+ : never
582
+ : false;
583
+ export type NumericString = '0123456789';
584
+ /**
585
+ Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
586
+
587
+ @example
588
+ ```
589
+ PositiveNumericStringGt<'500', '1'>;
590
+ //=> true
591
+
592
+ PositiveNumericStringGt<'1', '1'>;
593
+ //=> false
594
+
595
+ PositiveNumericStringGt<'1', '500'>;
596
+ //=> false
597
+ ```
598
+ */
599
+ export type PositiveNumericStringGt<
600
+ A extends string,
601
+ B extends string,
602
+ > = A extends B
603
+ ? false
604
+ : [
605
+ BuildTuple<StringLength<A>, 0>,
606
+ BuildTuple<StringLength<B>, 0>,
607
+ ] extends infer R extends [readonly unknown[], readonly unknown[]]
608
+ ? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
609
+ ? 0 extends Remain['length']
610
+ ? SameLengthPositiveNumericStringGt<A, B>
611
+ : true
612
+ : false
613
+ : never;
614
+ /**
615
+ Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
616
+
617
+ @example
618
+ ```
619
+ PositiveNumericCharacterGt<'5', '1'>;
620
+ //=> true
621
+
622
+ PositiveNumericCharacterGt<'1', '1'>;
623
+ //=> false
624
+ ```
625
+ */
626
+ export type PositiveNumericCharacterGt<
627
+ A extends string,
628
+ B extends string,
629
+ > = NumericString extends `${infer HeadA}${A}${infer TailA}`
630
+ ? NumericString extends `${infer HeadB}${B}${infer TailB}`
631
+ ? HeadA extends `${HeadB}${infer _}${infer __}`
632
+ ? true
633
+ : false
634
+ : never
635
+ : never;
636
+ /**
637
+ Returns the absolute value of a given value.
638
+
639
+ @example
640
+ ```
641
+ NumberAbsolute<-1>;
642
+ //=> 1
643
+
644
+ NumberAbsolute<1>;
645
+ //=> 1
646
+
647
+ NumberAbsolute<NegativeInfinity>
648
+ //=> PositiveInfinity
649
+ ```
650
+ */
651
+ export type NumberAbsolute<N extends number> =
652
+ `${N}` extends `-${infer StringPositiveN}`
653
+ ? StringToNumber<StringPositiveN>
654
+ : N;
655
+ /**
656
+ Check whether the given type is a number or a number string.
657
+
658
+ Supports floating-point as a string.
659
+
660
+ @example
661
+ ```
662
+ type A = IsNumberLike<'1'>;
663
+ //=> true
664
+
665
+ type B = IsNumberLike<'-1.1'>;
666
+ //=> true
667
+
668
+ type C = IsNumberLike<1>;
669
+ //=> true
670
+
671
+ type D = IsNumberLike<'a'>;
672
+ //=> false
673
+ */
674
+ export type IsNumberLike<N> = N extends number
675
+ ? true
676
+ : N extends `${number}`
677
+ ? true
678
+ : N extends `${number}.${number}`
679
+ ? true
680
+ : false;
681
+ /**
682
+ Matches any primitive, `void`, `Date`, or `RegExp` value.
683
+ */
684
+ export type BuiltIns = Primitive | void | Date | RegExp;
685
+ /**
686
+ Matches non-recursive types.
687
+ */
688
+ export type NonRecursiveType =
689
+ | BuiltIns
690
+ | Function
691
+ | (new (
692
+ ...arguments_: any[]
693
+ ) => unknown);
694
+ /**
695
+ Returns the sum of two numbers.
696
+
697
+ Note:
698
+ - A or B can only support `-999` ~ `999`.
699
+ - A and B can only be small integers, less than 1000.
700
+ - If the result is negative, you can only get `number`.
701
+
702
+ @example
703
+ ```
704
+ import type {Sum} from 'type-fest';
705
+
706
+ Sum<111, 222>;
707
+ //=> 333
708
+
709
+ Sum<-111, 222>;
710
+ //=> 111
711
+
712
+ Sum<111, -222>;
713
+ //=> number
714
+
715
+ Sum<PositiveInfinity, -9999>;
716
+ //=> PositiveInfinity
717
+
718
+ Sum<PositiveInfinity, NegativeInfinity>;
719
+ //=> number
720
+ ```
721
+
722
+ @category Numeric
723
+ */
724
+ // TODO: Support big integer and negative number.
725
+ export type Sum<A extends number, B extends number> = number extends A | B
726
+ ? number
727
+ : [
728
+ IsEqual<A, PositiveInfinity>,
729
+ IsEqual<A, NegativeInfinity>,
730
+ IsEqual<B, PositiveInfinity>,
731
+ IsEqual<B, NegativeInfinity>,
732
+ ] extends infer R extends [boolean, boolean, boolean, boolean]
733
+ ? Or<
734
+ And<IsEqual<R[0], true>, IsEqual<R[3], false>>,
735
+ And<IsEqual<R[2], true>, IsEqual<R[1], false>>
736
+ > extends true
737
+ ? PositiveInfinity
738
+ : Or<
739
+ And<IsEqual<R[1], true>, IsEqual<R[2], false>>,
740
+ And<IsEqual<R[3], true>, IsEqual<R[0], false>>
741
+ > extends true
742
+ ? NegativeInfinity
743
+ : true extends R[number]
744
+ ? number
745
+ : ([IsNegative<A>, IsNegative<B>] extends infer R
746
+ ? [false, false] extends R
747
+ ? [...BuildTuple<A>, ...BuildTuple<B>]['length']
748
+ : [true, true] extends R
749
+ ? number
750
+ : TupleMax<
751
+ [NumberAbsolute<A>, NumberAbsolute<B>]
752
+ > extends infer Max_
753
+ ? TupleMin<
754
+ [NumberAbsolute<A>, NumberAbsolute<B>]
755
+ > extends infer Min_ extends number
756
+ ? Max_ extends A | B
757
+ ? Subtract<Max_, Min_>
758
+ : number
759
+ : never
760
+ : never
761
+ : never) &
762
+ number
763
+ : never;
764
+ /**
765
+ Returns the difference between two numbers.
766
+
767
+ Note:
768
+ - A or B can only support `-999` ~ `999`.
769
+ - If the result is negative, you can only get `number`.
770
+
771
+ @example
772
+ ```
773
+ import type {Subtract} from 'type-fest';
774
+
775
+ Subtract<333, 222>;
776
+ //=> 111
777
+
778
+ Subtract<111, -222>;
779
+ //=> 333
780
+
781
+ Subtract<-111, 222>;
782
+ //=> number
783
+
784
+ Subtract<PositiveInfinity, 9999>;
785
+ //=> PositiveInfinity
786
+
787
+ Subtract<PositiveInfinity, PositiveInfinity>;
788
+ //=> number
789
+ ```
790
+
791
+ @category Numeric
792
+ */
793
+ // TODO: Support big integer and negative number.
794
+ export type Subtract<A extends number, B extends number> = number extends A | B
795
+ ? number
796
+ : [
797
+ IsEqual<A, PositiveInfinity>,
798
+ IsEqual<A, NegativeInfinity>,
799
+ IsEqual<B, PositiveInfinity>,
800
+ IsEqual<B, NegativeInfinity>,
801
+ ] extends infer R extends [boolean, boolean, boolean, boolean]
802
+ ? Or<
803
+ And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
804
+ And<IsEqual<R[3], true>, IsEqual<R[1], false>>
805
+ > extends true
806
+ ? PositiveInfinity
807
+ : Or<
808
+ And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
809
+ And<IsEqual<R[2], true>, IsEqual<R[0], false>>
810
+ > extends true
811
+ ? NegativeInfinity
812
+ : true extends R[number]
813
+ ? number
814
+ : [IsNegative<A>, IsNegative<B>] extends infer R
815
+ ? [false, false] extends R
816
+ ? BuildTuple<A> extends infer R
817
+ ? R extends [...BuildTuple<B>, ...infer R]
818
+ ? R['length']
819
+ : number
820
+ : never
821
+ : LessThan<A, B> extends true
822
+ ? number
823
+ : [false, true] extends R
824
+ ? Sum<A, NumberAbsolute<B>>
825
+ : Subtract<NumberAbsolute<B>, NumberAbsolute<A>>
826
+ : never
827
+ : never;
828
+ /**
829
+ Paths options.
830
+
831
+ @see {@link Paths}
832
+ */
833
+ export type PathsOptions = {
834
+ /**
835
+ The maximum depth to recurse when searching for paths.
836
+
837
+ @default 10
838
+ */
839
+ maxRecursionDepth?: number;
840
+ /**
841
+ Use bracket notation for array indices and numeric object keys.
842
+
843
+ @default false
844
+
845
+ @example
846
+ ```
847
+ type ArrayExample = {
848
+ array: ['foo'];
849
+ };
850
+
851
+ type A = Paths<ArrayExample, {bracketNotation: false}>;
852
+ //=> 'array' | 'array.0'
853
+
854
+ type B = Paths<ArrayExample, {bracketNotation: true}>;
855
+ //=> 'array' | 'array[0]'
856
+ ```
857
+
858
+ @example
859
+ ```
860
+ type NumberKeyExample = {
861
+ 1: ['foo'];
862
+ };
863
+
864
+ type A = Paths<NumberKeyExample, {bracketNotation: false}>;
865
+ //=> 1 | '1' | '1.0'
866
+
867
+ type B = Paths<NumberKeyExample, {bracketNotation: true}>;
868
+ //=> '[1]' | '[1][0]'
869
+ ```
870
+ */
871
+ bracketNotation?: boolean;
872
+ };
873
+ export type DefaultPathsOptions = {
874
+ maxRecursionDepth: 10;
875
+ bracketNotation: false;
876
+ };
877
+ /**
878
+ Generate a union of all possible paths to properties in the given object.
879
+
880
+ It also works with arrays.
881
+
882
+ Use-case: You want a type-safe way to access deeply nested properties in an object.
883
+
884
+ @example
885
+ ```
886
+ import type {Paths} from 'type-fest';
887
+
888
+ type Project = {
889
+ filename: string;
890
+ listA: string[];
891
+ listB: [{filename: string}];
892
+ folder: {
893
+ subfolder: {
894
+ filename: string;
895
+ };
896
+ };
897
+ };
898
+
899
+ type ProjectPaths = Paths<Project>;
900
+ //=> 'filename' | 'listA' | 'listB' | 'folder' | `listA.${number}` | 'listB.0' | 'listB.0.filename' | 'folder.subfolder' | 'folder.subfolder.filename'
901
+
902
+ declare function open<Path extends ProjectPaths>(path: Path): void;
903
+
904
+ open('filename'); // Pass
905
+ open('folder.subfolder'); // Pass
906
+ open('folder.subfolder.filename'); // Pass
907
+ open('foo'); // TypeError
908
+
909
+ // Also works with arrays
910
+ open('listA.1'); // Pass
911
+ open('listB.0'); // Pass
912
+ open('listB.1'); // TypeError. Because listB only has one element.
913
+ ```
914
+
915
+ @category Object
916
+ @category Array
917
+ */
918
+ export type Paths<T, Options extends PathsOptions = {}> = _Paths<
919
+ T,
920
+ {
921
+ // Set default maxRecursionDepth to 10
922
+ maxRecursionDepth: Options['maxRecursionDepth'] extends number
923
+ ? Options['maxRecursionDepth']
924
+ : DefaultPathsOptions['maxRecursionDepth'];
925
+ // Set default bracketNotation to false
926
+ bracketNotation: Options['bracketNotation'] extends boolean
927
+ ? Options['bracketNotation']
928
+ : DefaultPathsOptions['bracketNotation'];
929
+ }
930
+ >;
931
+ export type _Paths<T, Options extends Required<PathsOptions>> = T extends
932
+ | NonRecursiveType
933
+ | ReadonlyMap<unknown, unknown>
934
+ | ReadonlySet<unknown>
935
+ ? never
936
+ : IsAny<T> extends true
937
+ ? never
938
+ : T extends UnknownArray
939
+ ? number extends T['length']
940
+ ?
941
+ | InternalPaths<StaticPartOfArray<T>, Options>
942
+ | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
943
+ : InternalPaths<T, Options>
944
+ : T extends object
945
+ ? InternalPaths<T, Options>
946
+ : never;
947
+ export type InternalPaths<
948
+ T,
949
+ Options extends Required<PathsOptions>,
950
+ > = Options['maxRecursionDepth'] extends infer MaxDepth extends number
951
+ ? Required<T> extends infer T
952
+ ? T extends EmptyObject | readonly []
953
+ ? never
954
+ : {
955
+ [Key in keyof T]: Key extends string | number // Limit `Key` to string or number.
956
+ ? (
957
+ Options['bracketNotation'] extends true
958
+ ? IsNumberLike<Key> extends true
959
+ ? `[${Key}]`
960
+ : Key | ToString<Key>
961
+ : never | Options['bracketNotation'] extends false
962
+ ? Key | ToString<Key>
963
+ : never
964
+ ) extends infer TranformedKey extends string | number
965
+ ? // 1. If style is 'a[0].b' and 'Key' is a numberlike value like 3 or '3', transform 'Key' to `[${Key}]`, else to `${Key}` | Key
966
+ // 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
967
+ | TranformedKey
968
+ // Recursively generate paths for the current key
969
+ | (GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
970
+ ? _Paths<
971
+ T[Key],
972
+ {
973
+ bracketNotation: Options['bracketNotation'];
974
+ maxRecursionDepth: Subtract<MaxDepth, 1>;
975
+ }
976
+ > extends infer SubPath
977
+ ? SubPath extends string | number
978
+ ?
979
+ | (Options['bracketNotation'] extends true
980
+ ? SubPath extends
981
+ | `[${any}]`
982
+ | `[${any}]${string}`
983
+ ? `${TranformedKey}${SubPath}` // If next node is number key like `[3]`, no need to add `.` before it.
984
+ : `${TranformedKey}.${SubPath}`
985
+ : never)
986
+ | (Options['bracketNotation'] extends false
987
+ ? `${TranformedKey}.${SubPath}`
988
+ : never)
989
+ : never
990
+ : never
991
+ : never)
992
+ : never
993
+ : never;
994
+ }[keyof T & (T extends UnknownArray ? number : unknown)]
995
+ : never
996
+ : never;
997
+ export type LiteralStringUnion<T> = LiteralUnion<T, string>;
998
+ /**
999
+ Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
1000
+
1001
+ Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
1002
+
1003
+ This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
1004
+
1005
+ @example
1006
+ ```
1007
+ import type {LiteralUnion} from 'type-fest';
1008
+
1009
+ // Before
1010
+
1011
+ type Pet = 'dog' | 'cat' | string;
1012
+
1013
+ const pet: Pet = '';
1014
+ // Start typing in your TypeScript-enabled IDE.
1015
+ // You **will not** get auto-completion for `dog` and `cat` literals.
1016
+
1017
+ // After
1018
+
1019
+ type Pet2 = LiteralUnion<'dog' | 'cat', string>;
1020
+
1021
+ const pet: Pet2 = '';
1022
+ // You **will** get auto-completion for `dog` and `cat` literals.
1023
+ ```
1024
+
1025
+ @category Type
1026
+ */
1027
+ export type LiteralUnion<LiteralType, BaseType extends Primitive> =
1028
+ | LiteralType
1029
+ | (BaseType & Record<never, never>);
1030
+ /**
1031
+ Get keys of the given type as strings.
1032
+
1033
+ Number keys are converted to strings.
1034
+
1035
+ Use-cases:
1036
+ - Get string keys from a type which may have number keys.
1037
+ - Makes it possible to index using strings retrieved from template types.
1038
+
1039
+ @example
1040
+ ```
1041
+ import type {StringKeyOf} from 'type-fest';
1042
+
1043
+ type Foo = {
1044
+ 1: number,
1045
+ stringKey: string,
1046
+ };
1047
+
1048
+ type StringKeysOfFoo = StringKeyOf<Foo>;
1049
+ //=> '1' | 'stringKey'
1050
+ ```
1051
+
1052
+ @category Object
1053
+ */
1054
+ export type StringKeyOf<BaseType> =
1055
+ `${Extract<keyof BaseType, string | number>}`;
1056
+ /**
1057
+ Represents an array of strings split using a given character or character set.
1058
+
1059
+ Use-case: Defining the return type of a method like `String.prototype.split`.
1060
+
1061
+ @example
1062
+ ```
1063
+ import type {Split} from 'type-fest';
1064
+
1065
+ declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;
1066
+
1067
+ type Item = 'foo' | 'bar' | 'baz' | 'waldo';
1068
+ const items = 'foo,bar,baz,waldo';
1069
+ let array: Item[];
1070
+
1071
+ array = split(items, ',');
1072
+ ```
1073
+
1074
+ @category String
1075
+ @category Template literal
1076
+ */
1077
+ export type Split<
1078
+ S extends string,
1079
+ Delimiter extends string,
1080
+ > = S extends `${infer Head}${Delimiter}${infer Tail}`
1081
+ ? [Head, ...Split<Tail, Delimiter>]
1082
+ : S extends Delimiter
1083
+ ? []
1084
+ : [S];
1085
+ export type GetOptions = {
1086
+ /**
1087
+ Include `undefined` in the return type when accessing properties.
1088
+
1089
+ Setting this to `false` is not recommended.
1090
+
1091
+ @default true
1092
+ */
1093
+ strict?: boolean;
1094
+ };
1095
+ /**
1096
+ Like the `Get` type but receives an array of strings as a path parameter.
1097
+ */
1098
+ export type GetWithPath<
1099
+ BaseType,
1100
+ Keys,
1101
+ Options extends GetOptions = {},
1102
+ > = Keys extends readonly []
1103
+ ? BaseType
1104
+ : Keys extends readonly [infer Head, ...infer Tail]
1105
+ ? GetWithPath<
1106
+ PropertyOf<BaseType, Extract<Head, string>, Options>,
1107
+ Extract<Tail, string[]>,
1108
+ Options
1109
+ >
1110
+ : never;
1111
+ /**
1112
+ Adds `undefined` to `Type` if `strict` is enabled.
1113
+ */
1114
+ export type Strictify<
1115
+ Type,
1116
+ Options extends GetOptions,
1117
+ > = Options['strict'] extends false ? Type : Type | undefined;
1118
+ /**
1119
+ If `Options['strict']` is `true`, includes `undefined` in the returned type when accessing properties on `Record<string, any>`.
1120
+
1121
+ Known limitations:
1122
+ - Does not include `undefined` in the type on object types with an index signature (for example, `{a: string; [key: string]: string}`).
1123
+ */
1124
+ export type StrictPropertyOf<
1125
+ BaseType,
1126
+ Key extends keyof BaseType,
1127
+ Options extends GetOptions,
1128
+ > = Record<string, any> extends BaseType
1129
+ ? string extends keyof BaseType
1130
+ ? Strictify<BaseType[Key], Options> // Record<string, any>
1131
+ : BaseType[Key] // Record<'a' | 'b', any> (Records with a string union as keys have required properties)
1132
+ : BaseType[Key];
1133
+ /**
1134
+ Splits a dot-prop style path into a tuple comprised of the properties in the path. Handles square-bracket notation.
1135
+
1136
+ @example
1137
+ ```
1138
+ ToPath<'foo.bar.baz'>
1139
+ //=> ['foo', 'bar', 'baz']
1140
+
1141
+ ToPath<'foo[0].bar.baz'>
1142
+ //=> ['foo', '0', 'bar', 'baz']
1143
+ ```
1144
+ */
1145
+ export type ToPath<S extends string> = Split<FixPathSquareBrackets<S>, '.'>;
1146
+ /**
1147
+ Replaces square-bracketed dot notation with dots, for example, `foo[0].bar` -> `foo.0.bar`.
1148
+ */
1149
+ export type FixPathSquareBrackets<Path extends string> =
1150
+ Path extends `[${infer Head}]${infer Tail}`
1151
+ ? Tail extends `[${string}`
1152
+ ? `${Head}.${FixPathSquareBrackets<Tail>}`
1153
+ : `${Head}${FixPathSquareBrackets<Tail>}`
1154
+ : Path extends `${infer Head}[${infer Middle}]${infer Tail}`
1155
+ ? `${Head}.${FixPathSquareBrackets<`[${Middle}]${Tail}`>}`
1156
+ : Path;
1157
+ /**
1158
+ Returns true if `LongString` is made up out of `Substring` repeated 0 or more times.
1159
+
1160
+ @example
1161
+ ```
1162
+ ConsistsOnlyOf<'aaa', 'a'> //=> true
1163
+ ConsistsOnlyOf<'ababab', 'ab'> //=> true
1164
+ ConsistsOnlyOf<'aBa', 'a'> //=> false
1165
+ ConsistsOnlyOf<'', 'a'> //=> true
1166
+ ```
1167
+ */
1168
+ export type ConsistsOnlyOf<
1169
+ LongString extends string,
1170
+ Substring extends string,
1171
+ > = LongString extends ''
1172
+ ? true
1173
+ : LongString extends `${Substring}${infer Tail}`
1174
+ ? ConsistsOnlyOf<Tail, Substring>
1175
+ : false;
1176
+ /**
1177
+ Convert a type which may have number keys to one with string keys, making it possible to index using strings retrieved from template types.
1178
+
1179
+ @example
1180
+ ```
1181
+ type WithNumbers = {foo: string; 0: boolean};
1182
+ type WithStrings = WithStringKeys<WithNumbers>;
1183
+
1184
+ type WithNumbersKeys = keyof WithNumbers;
1185
+ //=> 'foo' | 0
1186
+ type WithStringsKeys = keyof WithStrings;
1187
+ //=> 'foo' | '0'
1188
+ ```
1189
+ */
1190
+ export type WithStringKeys<BaseType> = {
1191
+ [Key in StringKeyOf<BaseType>]: UncheckedIndex<BaseType, Key>;
1192
+ };
1193
+ /**
1194
+ Perform a `T[U]` operation if `T` supports indexing.
1195
+ */
1196
+ export type UncheckedIndex<T, U extends string | number> = [T] extends [
1197
+ Record<string | number, any>,
1198
+ ]
1199
+ ? T[U]
1200
+ : never;
1201
+ /**
1202
+ Get a property of an object or array. Works when indexing arrays using number-literal-strings, for example, `PropertyOf<number[], '0'> = number`, and when indexing objects with number keys.
1203
+
1204
+ Note:
1205
+ - Returns `unknown` if `Key` is not a property of `BaseType`, since TypeScript uses structural typing, and it cannot be guaranteed that extra properties unknown to the type system will exist at runtime.
1206
+ - Returns `undefined` from nullish values, to match the behaviour of most deep-key libraries like `lodash`, `dot-prop`, etc.
1207
+ */
1208
+ export type PropertyOf<
1209
+ BaseType,
1210
+ Key extends string,
1211
+ Options extends GetOptions = {},
1212
+ > = BaseType extends null | undefined
1213
+ ? undefined
1214
+ : Key extends keyof BaseType
1215
+ ? StrictPropertyOf<BaseType, Key, Options>
1216
+ : BaseType extends readonly [] | readonly [unknown, ...unknown[]]
1217
+ ? unknown // It's a tuple, but `Key` did not extend `keyof BaseType`. So the index is out of bounds.
1218
+ : BaseType extends {
1219
+ [n: number]: infer Item;
1220
+ length: number; // Note: This is needed to avoid being too lax with records types using number keys like `{0: string; 1: boolean}`.
1221
+ }
1222
+ ? ConsistsOnlyOf<Key, StringDigit> extends true
1223
+ ? Strictify<Item, Options>
1224
+ : unknown
1225
+ : Key extends keyof WithStringKeys<BaseType>
1226
+ ? StrictPropertyOf<WithStringKeys<BaseType>, Key, Options>
1227
+ : unknown;
1228
+ // 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.
1229
+ /**
1230
+ Get a deeply-nested property from an object using a key path, like Lodash's `.get()` function.
1231
+
1232
+ Use-case: Retrieve a property from deep inside an API response or some other complex object.
1233
+
1234
+ @example
1235
+ ```
1236
+ import type {Get} from 'type-fest';
1237
+ import * as lodash from 'lodash';
1238
+
1239
+ const get = <BaseType, Path extends string | readonly string[]>(object: BaseType, path: Path): Get<BaseType, Path> =>
1240
+ lodash.get(object, path);
1241
+
1242
+ interface ApiResponse {
1243
+ hits: {
1244
+ hits: Array<{
1245
+ _id: string
1246
+ _source: {
1247
+ name: Array<{
1248
+ given: string[]
1249
+ family: string
1250
+ }>
1251
+ birthDate: string
1252
+ }
1253
+ }>
1254
+ }
1255
+ }
1256
+
1257
+ const getName = (apiResponse: ApiResponse) =>
1258
+ get(apiResponse, 'hits.hits[0]._source.name');
1259
+ //=> Array<{given: string[]; family: string}> | undefined
1260
+
1261
+ // Path also supports a readonly array of strings
1262
+ const getNameWithPathArray = (apiResponse: ApiResponse) =>
1263
+ get(apiResponse, ['hits','hits', '0', '_source', 'name'] as const);
1264
+ //=> Array<{given: string[]; family: string}> | undefined
1265
+
1266
+ // Non-strict mode:
1267
+ Get<string[], '3', {strict: false}> //=> string
1268
+ Get<Record<string, string>, 'foo', {strict: true}> // => string
1269
+ ```
1270
+
1271
+ @category Object
1272
+ @category Array
1273
+ @category Template literal
1274
+ */
1275
+ export type Get<
1276
+ BaseType,
1277
+ Path extends
1278
+ | readonly string[]
1279
+ | LiteralStringUnion<
1280
+ ToString<
1281
+ | Paths<
1282
+ BaseType,
1283
+ {
1284
+ bracketNotation: false;
1285
+ }
1286
+ >
1287
+ | Paths<
1288
+ BaseType,
1289
+ {
1290
+ bracketNotation: true;
1291
+ }
1292
+ >
1293
+ >
1294
+ >,
1295
+ Options extends GetOptions = {},
1296
+ > = GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
1297
+ export type PlainObject = UnknownRecord;
1298
+ /**
1299
+ * - Get the value from an object using a known path
1300
+ * - You can retrieve a nested value by using dot notation, e.g., `foo.bar.baz`
1301
+ * - Returns `undefined` if the value is not found
1302
+ */
1303
+ export declare function getValue<
1304
+ Data extends PlainObject,
1305
+ Path extends Paths<Data>,
1306
+ >(data: Data, path: Path): Get<Data, ToString<Path>>;
1307
+ /**
1308
+ * - Get the value from an object using an unknown path
1309
+ * - You can retrieve a nested value by using dot notation, e.g., `foo.bar.baz`
1310
+ * - If `ignoreCase` is `true`, path matching will be case-insensitive
1311
+ * - Returns `undefined` if the value is not found
1312
+ */
1313
+ export declare function getValue<Data extends PlainObject>(
1314
+ data: Data,
1315
+ path: string,
1316
+ ignoreCase?: boolean,
1317
+ ): unknown;
1318
+
1319
+ export {};