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