ts-data-forge 1.0.0 → 1.0.1

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 (292) hide show
  1. package/README.md +1 -1
  2. package/dist/array/array-utils.d.mts +2617 -0
  3. package/dist/array/array-utils.d.mts.map +1 -0
  4. package/dist/array/array-utils.mjs +2915 -0
  5. package/dist/array/array-utils.mjs.map +1 -0
  6. package/dist/array/index.d.mts +3 -0
  7. package/dist/array/index.d.mts.map +1 -0
  8. package/dist/array/index.mjs +3 -0
  9. package/dist/array/index.mjs.map +1 -0
  10. package/dist/array/tuple-utils.d.mts +421 -0
  11. package/dist/array/tuple-utils.d.mts.map +1 -0
  12. package/dist/array/tuple-utils.mjs +391 -0
  13. package/dist/array/tuple-utils.mjs.map +1 -0
  14. package/dist/collections/imap-mapped.d.mts +445 -0
  15. package/dist/collections/imap-mapped.d.mts.map +1 -0
  16. package/dist/collections/imap-mapped.mjs +424 -0
  17. package/dist/collections/imap-mapped.mjs.map +1 -0
  18. package/dist/collections/imap.d.mts +359 -0
  19. package/dist/collections/imap.d.mts.map +1 -0
  20. package/dist/collections/imap.mjs +338 -0
  21. package/dist/collections/imap.mjs.map +1 -0
  22. package/dist/collections/index.d.mts +7 -0
  23. package/dist/collections/index.d.mts.map +1 -0
  24. package/dist/collections/index.mjs +7 -0
  25. package/dist/collections/index.mjs.map +1 -0
  26. package/dist/collections/iset-mapped.d.mts +576 -0
  27. package/dist/collections/iset-mapped.d.mts.map +1 -0
  28. package/dist/collections/iset-mapped.mjs +522 -0
  29. package/dist/collections/iset-mapped.mjs.map +1 -0
  30. package/dist/collections/iset.d.mts +426 -0
  31. package/dist/collections/iset.d.mts.map +1 -0
  32. package/dist/collections/iset.mjs +437 -0
  33. package/dist/collections/iset.mjs.map +1 -0
  34. package/dist/collections/queue.d.mts +190 -0
  35. package/dist/collections/queue.d.mts.map +1 -0
  36. package/dist/collections/queue.mjs +317 -0
  37. package/dist/collections/queue.mjs.map +1 -0
  38. package/dist/collections/stack.d.mts +210 -0
  39. package/dist/collections/stack.d.mts.map +1 -0
  40. package/dist/collections/stack.mjs +353 -0
  41. package/dist/collections/stack.mjs.map +1 -0
  42. package/dist/expect-type.d.mts +199 -0
  43. package/dist/expect-type.d.mts.map +1 -0
  44. package/dist/expect-type.mjs +201 -0
  45. package/dist/expect-type.mjs.map +1 -0
  46. package/dist/functional/index.d.mts +5 -0
  47. package/dist/functional/index.d.mts.map +1 -0
  48. package/dist/functional/index.mjs +5 -0
  49. package/dist/functional/index.mjs.map +1 -0
  50. package/dist/functional/match.d.mts +215 -0
  51. package/dist/functional/match.d.mts.map +1 -0
  52. package/dist/functional/match.mjs +139 -0
  53. package/dist/functional/match.mjs.map +1 -0
  54. package/dist/functional/optional.d.mts +517 -0
  55. package/dist/functional/optional.d.mts.map +1 -0
  56. package/dist/functional/optional.mjs +532 -0
  57. package/dist/functional/optional.mjs.map +1 -0
  58. package/dist/functional/pipe.d.mts +185 -0
  59. package/dist/functional/pipe.d.mts.map +1 -0
  60. package/dist/functional/pipe.mjs +129 -0
  61. package/dist/functional/pipe.mjs.map +1 -0
  62. package/dist/functional/result.d.mts +796 -0
  63. package/dist/functional/result.d.mts.map +1 -0
  64. package/dist/functional/result.mjs +844 -0
  65. package/dist/functional/result.mjs.map +1 -0
  66. package/dist/globals.d.mts +38 -0
  67. package/dist/guard/has-key.d.mts +100 -0
  68. package/dist/guard/has-key.d.mts.map +1 -0
  69. package/dist/guard/has-key.mjs +94 -0
  70. package/dist/guard/has-key.mjs.map +1 -0
  71. package/dist/guard/index.d.mts +8 -0
  72. package/dist/guard/index.d.mts.map +1 -0
  73. package/dist/guard/index.mjs +8 -0
  74. package/dist/guard/index.mjs.map +1 -0
  75. package/dist/guard/is-non-empty-string.d.mts +106 -0
  76. package/dist/guard/is-non-empty-string.d.mts.map +1 -0
  77. package/dist/guard/is-non-empty-string.mjs +108 -0
  78. package/dist/guard/is-non-empty-string.mjs.map +1 -0
  79. package/dist/guard/is-non-null-object.d.mts +105 -0
  80. package/dist/guard/is-non-null-object.d.mts.map +1 -0
  81. package/dist/guard/is-non-null-object.mjs +108 -0
  82. package/dist/guard/is-non-null-object.mjs.map +1 -0
  83. package/dist/guard/is-primitive.d.mts +146 -0
  84. package/dist/guard/is-primitive.d.mts.map +1 -0
  85. package/dist/guard/is-primitive.mjs +161 -0
  86. package/dist/guard/is-primitive.mjs.map +1 -0
  87. package/dist/guard/is-record.d.mts +151 -0
  88. package/dist/guard/is-record.d.mts.map +1 -0
  89. package/dist/guard/is-record.mjs +155 -0
  90. package/dist/guard/is-record.mjs.map +1 -0
  91. package/dist/guard/is-type.d.mts +430 -0
  92. package/dist/guard/is-type.d.mts.map +1 -0
  93. package/dist/guard/is-type.mjs +432 -0
  94. package/dist/guard/is-type.mjs.map +1 -0
  95. package/dist/guard/key-is-in.d.mts +158 -0
  96. package/dist/guard/key-is-in.d.mts.map +1 -0
  97. package/dist/guard/key-is-in.mjs +160 -0
  98. package/dist/guard/key-is-in.mjs.map +1 -0
  99. package/dist/index.d.mts +11 -0
  100. package/dist/index.d.mts.map +1 -0
  101. package/dist/index.mjs +61 -0
  102. package/dist/index.mjs.map +1 -0
  103. package/dist/iterator/index.d.mts +2 -0
  104. package/dist/iterator/index.d.mts.map +1 -0
  105. package/dist/iterator/index.mjs +2 -0
  106. package/dist/iterator/index.mjs.map +1 -0
  107. package/dist/iterator/range.d.mts +97 -0
  108. package/dist/iterator/range.d.mts.map +1 -0
  109. package/dist/iterator/range.mjs +130 -0
  110. package/dist/iterator/range.mjs.map +1 -0
  111. package/dist/json/index.d.mts +2 -0
  112. package/dist/json/index.d.mts.map +1 -0
  113. package/dist/json/index.mjs +2 -0
  114. package/dist/json/index.mjs.map +1 -0
  115. package/dist/json/json.d.mts +597 -0
  116. package/dist/json/json.d.mts.map +1 -0
  117. package/dist/json/json.mjs +687 -0
  118. package/dist/json/json.mjs.map +1 -0
  119. package/dist/number/branded-types/finite-number.d.mts +291 -0
  120. package/dist/number/branded-types/finite-number.d.mts.map +1 -0
  121. package/dist/number/branded-types/finite-number.mjs +296 -0
  122. package/dist/number/branded-types/finite-number.mjs.map +1 -0
  123. package/dist/number/branded-types/index.d.mts +27 -0
  124. package/dist/number/branded-types/index.d.mts.map +1 -0
  125. package/dist/number/branded-types/index.mjs +27 -0
  126. package/dist/number/branded-types/index.mjs.map +1 -0
  127. package/dist/number/branded-types/int.d.mts +242 -0
  128. package/dist/number/branded-types/int.d.mts.map +1 -0
  129. package/dist/number/branded-types/int.mjs +239 -0
  130. package/dist/number/branded-types/int.mjs.map +1 -0
  131. package/dist/number/branded-types/int16.d.mts +162 -0
  132. package/dist/number/branded-types/int16.d.mts.map +1 -0
  133. package/dist/number/branded-types/int16.mjs +141 -0
  134. package/dist/number/branded-types/int16.mjs.map +1 -0
  135. package/dist/number/branded-types/int32.d.mts +155 -0
  136. package/dist/number/branded-types/int32.d.mts.map +1 -0
  137. package/dist/number/branded-types/int32.mjs +142 -0
  138. package/dist/number/branded-types/int32.mjs.map +1 -0
  139. package/dist/number/branded-types/non-negative-finite-number.d.mts +165 -0
  140. package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -0
  141. package/dist/number/branded-types/non-negative-finite-number.mjs +160 -0
  142. package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -0
  143. package/dist/number/branded-types/non-negative-int16.d.mts +160 -0
  144. package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -0
  145. package/dist/number/branded-types/non-negative-int16.mjs +138 -0
  146. package/dist/number/branded-types/non-negative-int16.mjs.map +1 -0
  147. package/dist/number/branded-types/non-negative-int32.d.mts +156 -0
  148. package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -0
  149. package/dist/number/branded-types/non-negative-int32.mjs +138 -0
  150. package/dist/number/branded-types/non-negative-int32.mjs.map +1 -0
  151. package/dist/number/branded-types/non-zero-finite-number.d.mts +154 -0
  152. package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -0
  153. package/dist/number/branded-types/non-zero-finite-number.mjs +160 -0
  154. package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -0
  155. package/dist/number/branded-types/non-zero-int.d.mts +131 -0
  156. package/dist/number/branded-types/non-zero-int.d.mts.map +1 -0
  157. package/dist/number/branded-types/non-zero-int.mjs +128 -0
  158. package/dist/number/branded-types/non-zero-int.mjs.map +1 -0
  159. package/dist/number/branded-types/non-zero-int16.d.mts +166 -0
  160. package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -0
  161. package/dist/number/branded-types/non-zero-int16.mjs +145 -0
  162. package/dist/number/branded-types/non-zero-int16.mjs.map +1 -0
  163. package/dist/number/branded-types/non-zero-int32.d.mts +158 -0
  164. package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -0
  165. package/dist/number/branded-types/non-zero-int32.mjs +145 -0
  166. package/dist/number/branded-types/non-zero-int32.mjs.map +1 -0
  167. package/dist/number/branded-types/non-zero-safe-int.d.mts +148 -0
  168. package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -0
  169. package/dist/number/branded-types/non-zero-safe-int.mjs +145 -0
  170. package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -0
  171. package/dist/number/branded-types/non-zero-uint16.d.mts +160 -0
  172. package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -0
  173. package/dist/number/branded-types/non-zero-uint16.mjs +140 -0
  174. package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -0
  175. package/dist/number/branded-types/non-zero-uint32.d.mts +156 -0
  176. package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -0
  177. package/dist/number/branded-types/non-zero-uint32.mjs +140 -0
  178. package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -0
  179. package/dist/number/branded-types/positive-finite-number.d.mts +171 -0
  180. package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -0
  181. package/dist/number/branded-types/positive-finite-number.mjs +165 -0
  182. package/dist/number/branded-types/positive-finite-number.mjs.map +1 -0
  183. package/dist/number/branded-types/positive-int.d.mts +270 -0
  184. package/dist/number/branded-types/positive-int.d.mts.map +1 -0
  185. package/dist/number/branded-types/positive-int.mjs +257 -0
  186. package/dist/number/branded-types/positive-int.mjs.map +1 -0
  187. package/dist/number/branded-types/positive-int16.d.mts +162 -0
  188. package/dist/number/branded-types/positive-int16.d.mts.map +1 -0
  189. package/dist/number/branded-types/positive-int16.mjs +139 -0
  190. package/dist/number/branded-types/positive-int16.mjs.map +1 -0
  191. package/dist/number/branded-types/positive-int32.d.mts +158 -0
  192. package/dist/number/branded-types/positive-int32.d.mts.map +1 -0
  193. package/dist/number/branded-types/positive-int32.mjs +139 -0
  194. package/dist/number/branded-types/positive-int32.mjs.map +1 -0
  195. package/dist/number/branded-types/positive-safe-int.d.mts +152 -0
  196. package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -0
  197. package/dist/number/branded-types/positive-safe-int.mjs +138 -0
  198. package/dist/number/branded-types/positive-safe-int.mjs.map +1 -0
  199. package/dist/number/branded-types/positive-uint16.d.mts +160 -0
  200. package/dist/number/branded-types/positive-uint16.d.mts.map +1 -0
  201. package/dist/number/branded-types/positive-uint16.mjs +139 -0
  202. package/dist/number/branded-types/positive-uint16.mjs.map +1 -0
  203. package/dist/number/branded-types/positive-uint32.d.mts +156 -0
  204. package/dist/number/branded-types/positive-uint32.d.mts.map +1 -0
  205. package/dist/number/branded-types/positive-uint32.mjs +139 -0
  206. package/dist/number/branded-types/positive-uint32.mjs.map +1 -0
  207. package/dist/number/branded-types/safe-int.d.mts +243 -0
  208. package/dist/number/branded-types/safe-int.d.mts.map +1 -0
  209. package/dist/number/branded-types/safe-int.mjs +240 -0
  210. package/dist/number/branded-types/safe-int.mjs.map +1 -0
  211. package/dist/number/branded-types/safe-uint.d.mts +151 -0
  212. package/dist/number/branded-types/safe-uint.d.mts.map +1 -0
  213. package/dist/number/branded-types/safe-uint.mjs +138 -0
  214. package/dist/number/branded-types/safe-uint.mjs.map +1 -0
  215. package/dist/number/branded-types/uint.d.mts +144 -0
  216. package/dist/number/branded-types/uint.d.mts.map +1 -0
  217. package/dist/number/branded-types/uint.mjs +132 -0
  218. package/dist/number/branded-types/uint.mjs.map +1 -0
  219. package/dist/number/branded-types/uint16.d.mts +157 -0
  220. package/dist/number/branded-types/uint16.d.mts.map +1 -0
  221. package/dist/number/branded-types/uint16.mjs +137 -0
  222. package/dist/number/branded-types/uint16.mjs.map +1 -0
  223. package/dist/number/branded-types/uint32.d.mts +185 -0
  224. package/dist/number/branded-types/uint32.d.mts.map +1 -0
  225. package/dist/number/branded-types/uint32.mjs +169 -0
  226. package/dist/number/branded-types/uint32.mjs.map +1 -0
  227. package/dist/number/enum/index.d.mts +3 -0
  228. package/dist/number/enum/index.d.mts.map +1 -0
  229. package/dist/number/enum/index.mjs +3 -0
  230. package/dist/number/enum/index.mjs.map +1 -0
  231. package/dist/number/enum/int8.d.mts +202 -0
  232. package/dist/number/enum/int8.d.mts.map +1 -0
  233. package/dist/number/enum/int8.mjs +296 -0
  234. package/dist/number/enum/int8.mjs.map +1 -0
  235. package/dist/number/enum/uint8.d.mts +128 -0
  236. package/dist/number/enum/uint8.d.mts.map +1 -0
  237. package/dist/number/enum/uint8.mjs +251 -0
  238. package/dist/number/enum/uint8.mjs.map +1 -0
  239. package/dist/number/index.d.mts +5 -0
  240. package/dist/number/index.d.mts.map +1 -0
  241. package/dist/number/index.mjs +31 -0
  242. package/dist/number/index.mjs.map +1 -0
  243. package/dist/number/num.d.mts +515 -0
  244. package/dist/number/num.d.mts.map +1 -0
  245. package/dist/number/num.mjs +513 -0
  246. package/dist/number/num.mjs.map +1 -0
  247. package/dist/number/refined-number-utils.d.mts +191 -0
  248. package/dist/number/refined-number-utils.d.mts.map +1 -0
  249. package/dist/number/refined-number-utils.mjs +179 -0
  250. package/dist/number/refined-number-utils.mjs.map +1 -0
  251. package/dist/object/index.d.mts +2 -0
  252. package/dist/object/index.d.mts.map +1 -0
  253. package/dist/object/index.mjs +2 -0
  254. package/dist/object/index.mjs.map +1 -0
  255. package/dist/object/object.d.mts +296 -0
  256. package/dist/object/object.d.mts.map +1 -0
  257. package/dist/object/object.mjs +295 -0
  258. package/dist/object/object.mjs.map +1 -0
  259. package/dist/others/cast-mutable.d.mts +110 -0
  260. package/dist/others/cast-mutable.d.mts.map +1 -0
  261. package/dist/others/cast-mutable.mjs +114 -0
  262. package/dist/others/cast-mutable.mjs.map +1 -0
  263. package/dist/others/cast-readonly.d.mts +189 -0
  264. package/dist/others/cast-readonly.d.mts.map +1 -0
  265. package/dist/others/cast-readonly.mjs +193 -0
  266. package/dist/others/cast-readonly.mjs.map +1 -0
  267. package/dist/others/if-then.d.mts +98 -0
  268. package/dist/others/if-then.d.mts.map +1 -0
  269. package/dist/others/if-then.mjs +100 -0
  270. package/dist/others/if-then.mjs.map +1 -0
  271. package/dist/others/index.d.mts +8 -0
  272. package/dist/others/index.d.mts.map +1 -0
  273. package/dist/others/index.mjs +8 -0
  274. package/dist/others/index.mjs.map +1 -0
  275. package/dist/others/map-nullable.d.mts +151 -0
  276. package/dist/others/map-nullable.d.mts.map +1 -0
  277. package/dist/others/map-nullable.mjs +159 -0
  278. package/dist/others/map-nullable.mjs.map +1 -0
  279. package/dist/others/memoize-function.d.mts +173 -0
  280. package/dist/others/memoize-function.d.mts.map +1 -0
  281. package/dist/others/memoize-function.mjs +189 -0
  282. package/dist/others/memoize-function.mjs.map +1 -0
  283. package/dist/others/tuple.d.mts +159 -0
  284. package/dist/others/tuple.d.mts.map +1 -0
  285. package/dist/others/tuple.mjs +161 -0
  286. package/dist/others/tuple.mjs.map +1 -0
  287. package/dist/others/unknown-to-string.d.mts +180 -0
  288. package/dist/others/unknown-to-string.d.mts.map +1 -0
  289. package/dist/others/unknown-to-string.mjs +211 -0
  290. package/dist/others/unknown-to-string.mjs.map +1 -0
  291. package/dist/tsconfig.json +1 -0
  292. package/package.json +16 -14
@@ -0,0 +1,796 @@
1
+ import { Optional } from './optional.mjs';
2
+ /** @internal Symbol to identify the 'Ok' variant of Result. */
3
+ declare const OkTypeSymbol: unique symbol;
4
+ /** @internal Symbol to identify the 'Err' variant of Result. */
5
+ declare const ErrTypeSymbol: unique symbol;
6
+ /**
7
+ * @internal
8
+ * Represents the 'Ok' variant of a Result, containing a success value.
9
+ * @template S The type of the success value.
10
+ */
11
+ type Ok_<S> = Readonly<{
12
+ /** Discriminant property for the 'Ok' type. */
13
+ type: typeof OkTypeSymbol;
14
+ /** The success value. */
15
+ value: S;
16
+ }>;
17
+ /**
18
+ * @internal
19
+ * Represents the 'Err' variant of a Result, containing an error value.
20
+ * @template E The type of the error value.
21
+ */
22
+ type Err_<E> = Readonly<{
23
+ /** Discriminant property for the 'Err' type. */
24
+ type: typeof ErrTypeSymbol;
25
+ /** The error value. */
26
+ value: E;
27
+ }>;
28
+ /**
29
+ * Represents a value that can either be a success (`Ok`) or an error (`Err`).
30
+ * @template S The type of the success value.
31
+ * @template E The type of the error value.
32
+ */
33
+ export type Result<S, E> = Ok_<S> | Err_<E>;
34
+ /**
35
+ * Namespace for the `Result` type and related functions.
36
+ * Provides utilities to handle operations that can succeed or fail.
37
+ */
38
+ export declare namespace Result {
39
+ /**
40
+ * Checks if the given value is a `Result`.
41
+ * @param maybeOptional The value to check.
42
+ * @returns `true` if the value is a `Result`, otherwise `false`.
43
+ */
44
+ export const isResult: (maybeOptional: unknown) => maybeOptional is Result<unknown, unknown>;
45
+ /**
46
+ * Represents a `Result` that is a success, containing a value.
47
+ * @template S The type of the success value.
48
+ */
49
+ export type Ok<S> = Ok_<S>;
50
+ /**
51
+ * Represents a `Result` that is an error, containing an error value.
52
+ * @template E The type of the error value.
53
+ */
54
+ export type Err<E> = Err_<E>;
55
+ /**
56
+ * Base type for any `Result`, used for generic constraints.
57
+ * Represents a `Result` with unknown success and error types.
58
+ */
59
+ export type Base = Result<unknown, unknown>;
60
+ /**
61
+ * Extracts the success value type `S` from a `Result.Ok<S>`.
62
+ * If the `Result` is `Result.Err<E>`, resolves to `never`.
63
+ * @template R The `Result.Base` type to unwrap.
64
+ */
65
+ export type UnwrapOk<R extends Base> = R extends Ok<infer S> ? S : never;
66
+ /**
67
+ * Extracts the error value type `E` from a `Result.Err<E>`.
68
+ * If the `Result` is `Result.Ok<S>`, resolves to `never`.
69
+ * @template R The `Result.Base` type to unwrap.
70
+ */
71
+ export type UnwrapErr<R extends Base> = R extends Err<infer E> ? E : never;
72
+ /**
73
+ * Narrows a `Result.Base` type to `Result.Ok<S>` if it is an `Ok`.
74
+ * If the `Result` is `Result.Err<E>`, resolves to `never`.
75
+ * @template R The `Result.Base` type to narrow.
76
+ */
77
+ export type NarrowToOk<R extends Base> = R extends Err<unknown> ? never : R;
78
+ /**
79
+ * Narrows a `Result.Base` type to `Result.Err<E>` if it is an `Err`.
80
+ * If the `Result` is `Result.Ok<S>`, resolves to `never`.
81
+ * @template R The `Result.Base` type to narrow.
82
+ */
83
+ export type NarrowToErr<R extends Base> = R extends Ok<unknown> ? never : R;
84
+ /**
85
+ * Creates a `Result.Ok` containing the given success value.
86
+ *
87
+ * Use this constructor when an operation succeeds and you want to wrap
88
+ * the successful result in a Result type for consistent error handling.
89
+ *
90
+ * @template S The type of the success value.
91
+ * @param value The success value.
92
+ * @returns A `Result.Ok<S>` containing the value.
93
+ * @example
94
+ * ```typescript
95
+ * // Basic success case
96
+ * const success = Result.ok(42);
97
+ * console.log(Result.isOk(success)); // true
98
+ * console.log(Result.unwrapOk(success)); // 42
99
+ *
100
+ * // Function that returns a Result
101
+ * function divide(a: number, b: number): Result<number, string> {
102
+ * if (b === 0) {
103
+ * return Result.err("Division by zero");
104
+ * }
105
+ * return Result.ok(a / b);
106
+ * }
107
+ *
108
+ * const result = divide(10, 2);
109
+ * console.log(Result.unwrapOk(result)); // 5
110
+ * ```
111
+ */
112
+ export const ok: <S>(value: S) => Ok<S>;
113
+ /**
114
+ * Creates a `Result.Err` containing the given error value.
115
+ *
116
+ * Use this constructor when an operation fails and you want to wrap
117
+ * the error information in a Result type for consistent error handling.
118
+ *
119
+ * @template E The type of the error value.
120
+ * @param value The error value.
121
+ * @returns A `Result.Err<E>` containing the value.
122
+ * @example
123
+ * ```typescript
124
+ * // Basic error case
125
+ * const failure = Result.err("Something went wrong");
126
+ * console.log(Result.isErr(failure)); // true
127
+ * console.log(Result.unwrapErr(failure)); // "Something went wrong"
128
+ *
129
+ * // Function that can fail
130
+ * function parseInteger(input: string): Result<number, string> {
131
+ * const num = parseInt(input, 10);
132
+ * if (isNaN(num)) {
133
+ * return Result.err(`Invalid number format: ${input}`);
134
+ * }
135
+ * return Result.ok(num);
136
+ * }
137
+ *
138
+ * const result = parseInteger("abc");
139
+ * console.log(Result.unwrapErr(result)); // "Invalid number format: abc"
140
+ *
141
+ * // Using custom error types
142
+ * interface ValidationError {
143
+ * field: string;
144
+ * message: string;
145
+ * }
146
+ *
147
+ * const validationError = Result.err<ValidationError>({
148
+ * field: "email",
149
+ * message: "Invalid email format"
150
+ * });
151
+ * ```
152
+ */
153
+ export const err: <E>(value: E) => Err<E>;
154
+ /**
155
+ * Checks if a `Result` is `Result.Ok`.
156
+ * Acts as a type guard, narrowing the type to the success variant.
157
+ *
158
+ * This function is essential for type-safe Result handling, allowing
159
+ * TypeScript to understand that subsequent operations will work with
160
+ * the success value rather than the error value.
161
+ *
162
+ * @template R The `Result.Base` type to check.
163
+ * @param result The `Result` to check.
164
+ * @returns `true` if the `Result` is `Result.Ok`, otherwise `false`.
165
+ * @example
166
+ * ```typescript
167
+ * // Basic type guard usage
168
+ * const result: Result<number, string> = divide(10, 2);
169
+ *
170
+ * if (Result.isOk(result)) {
171
+ * // TypeScript knows result is Result.Ok<number>
172
+ * console.log(result.value); // Safe to access .value
173
+ * console.log(Result.unwrapOk(result)); // 5
174
+ * } else {
175
+ * // TypeScript knows result is Result.Err<string>
176
+ * console.log(result.value); // Error message
177
+ * }
178
+ *
179
+ * // Using in conditional logic
180
+ * const processResult = (r: Result<string, Error>) => {
181
+ * return Result.isOk(r)
182
+ * ? r.value.toUpperCase() // Safe string operations
183
+ * : "Error occurred";
184
+ * };
185
+ *
186
+ * // Filtering arrays of Results
187
+ * const results: Result<number, string>[] = [
188
+ * Result.ok(1),
189
+ * Result.err("error"),
190
+ * Result.ok(2)
191
+ * ];
192
+ * const successes = results.filter(Result.isOk);
193
+ * // successes is Result.Ok<number>[]
194
+ * ```
195
+ */
196
+ export const isOk: <R extends Base>(result: R) => result is NarrowToOk<R>;
197
+ /**
198
+ * Checks if a `Result` is `Result.Err`.
199
+ * Acts as a type guard, narrowing the type to the error variant.
200
+ *
201
+ * This function is essential for type-safe Result handling, allowing
202
+ * TypeScript to understand that subsequent operations will work with
203
+ * the error value rather than the success value.
204
+ *
205
+ * @template R The `Result.Base` type to check.
206
+ * @param result The `Result` to check.
207
+ * @returns `true` if the `Result` is `Result.Err`, otherwise `false`.
208
+ * @example
209
+ * ```typescript
210
+ * // Basic type guard usage
211
+ * const result: Result<number, string> = divide(10, 0);
212
+ *
213
+ * if (Result.isErr(result)) {
214
+ * // TypeScript knows result is Result.Err<string>
215
+ * console.log(result.value); // Safe to access error .value
216
+ * console.log(Result.unwrapErr(result)); // "Division by zero"
217
+ * } else {
218
+ * // TypeScript knows result is Result.Ok<number>
219
+ * console.log(result.value); // Success value
220
+ * }
221
+ *
222
+ * // Error handling patterns
223
+ * const handleResult = (r: Result<Data, ApiError>) => {
224
+ * if (Result.isErr(r)) {
225
+ * logError(r.value); // Safe error operations
226
+ * return null;
227
+ * }
228
+ * return processData(r.value);
229
+ * };
230
+ *
231
+ * // Collecting errors from multiple Results
232
+ * const results: Result<string, ValidationError>[] = validateForm();
233
+ * const errors = results
234
+ * .filter(Result.isErr)
235
+ * .map(err => err.value); // ValidationError[]
236
+ * ```
237
+ */
238
+ export const isErr: <R extends Base>(result: R) => result is NarrowToErr<R>;
239
+ /**
240
+ * Unwraps a `Result`, returning the success value.
241
+ * Throws an error if the `Result` is `Result.Err`.
242
+ *
243
+ * This is useful when you're confident that a Result should contain a success value
244
+ * and want to treat errors as exceptional conditions. The error message will be
245
+ * constructed from the error value using the provided string conversion function.
246
+ *
247
+ * @template R The `Result.Base` type to unwrap.
248
+ * @param result The `Result` to unwrap.
249
+ * @param toStr An optional function to convert the error value to a string for the error message. Defaults to `String`.
250
+ * @returns The success value if `Result.Ok`.
251
+ * @throws {Error} Error with the stringified error value if the `Result` is `Result.Err`.
252
+ * @example
253
+ * ```typescript
254
+ * // Basic usage with default string conversion
255
+ * const success = Result.ok(42);
256
+ * console.log(Result.unwrapThrow(success)); // 42
257
+ *
258
+ * const failure = Result.err("Network error");
259
+ * try {
260
+ * Result.unwrapThrow(failure); // throws Error: "Network error"
261
+ * } catch (error) {
262
+ * console.log(error.message); // "Network error"
263
+ * }
264
+ *
265
+ * // Custom error string conversion
266
+ * interface ApiError {
267
+ * code: number;
268
+ * message: string;
269
+ * }
270
+ *
271
+ * const apiResult = Result.err<ApiError>({ code: 404, message: "Not found" });
272
+ * try {
273
+ * Result.unwrapThrow(apiResult, err => `API Error ${err.code}: ${err.message}`);
274
+ * } catch (error) {
275
+ * console.log(error.message); // "API Error 404: Not found"
276
+ * }
277
+ *
278
+ * // In contexts where failure is unexpected
279
+ * const configResult = loadConfiguration();
280
+ * const config = Result.unwrapThrow(configResult, err =>
281
+ * `Failed to load configuration: ${err}`
282
+ * ); // Will throw if config loading fails
283
+ * ```
284
+ */
285
+ export const unwrapThrow: <R extends Base>(result: R, toStr?: (e: UnwrapErr<R>) => string) => UnwrapOk<R>;
286
+ /**
287
+ * Unwraps a `Result`, returning the success value or `undefined` if it's an error.
288
+ *
289
+ * This function provides a safe way to extract success values from Results without
290
+ * throwing exceptions. It has overloaded behavior based on the type:
291
+ * - For `Result.Ok<T>`: Always returns `T` (guaranteed by type system)
292
+ * - For general `Result<T, E>`: Returns `T | undefined`
293
+ *
294
+ * @template R The `Result.Base` type to unwrap.
295
+ * @param result The `Result` to unwrap.
296
+ * @returns The success value if `Result.Ok`, otherwise `undefined`.
297
+ * @example
298
+ * ```typescript
299
+ * // With guaranteed Ok - returns the value
300
+ * const success = Result.ok(42);
301
+ * const value = Result.unwrapOk(success); // Type: number, Value: 42
302
+ *
303
+ * // With general Result - may return undefined
304
+ * const maybeResult: Result<string, Error> = fetchData();
305
+ * const data = Result.unwrapOk(maybeResult); // Type: string | undefined
306
+ *
307
+ * // Safe pattern for handling both cases
308
+ * const result = Result.ok("hello");
309
+ * const unwrapped = Result.unwrapOk(result);
310
+ * if (unwrapped !== undefined) {
311
+ * console.log(unwrapped.toUpperCase()); // "HELLO"
312
+ * }
313
+ *
314
+ * // Useful in conditional chains
315
+ * const processResult = (r: Result<number, string>) => {
316
+ * const value = Result.unwrapOk(r);
317
+ * return value !== undefined ? value * 2 : 0;
318
+ * };
319
+ * ```
320
+ */
321
+ export const unwrapOk: UnwrapOkFnOverload;
322
+ type UnwrapOkFnOverload = {
323
+ <R extends Ok<unknown>>(result: R): UnwrapOk<R>;
324
+ <R extends Base>(result: R): UnwrapOk<R> | undefined;
325
+ };
326
+ /**
327
+ * Unwraps a `Result`, returning the success value or a default value if it is `Result.Err`.
328
+ * @template R The `Result.Base` type to unwrap.
329
+ * @template D The type of the default value.
330
+ * @param result The `Result` to unwrap.
331
+ * @param defaultValue The value to return if `result` is `Result.Err`.
332
+ * @returns The success value if `Result.Ok`, otherwise `defaultValue`.
333
+ * @example
334
+ * ```typescript
335
+ * // Regular usage
336
+ * const result = Result.ok(42);
337
+ * const value = Result.unwrapOkOr(result, 0);
338
+ * console.log(value); // 42
339
+ *
340
+ * // Curried usage for pipe composition
341
+ * const unwrapWithDefault = Result.unwrapOkOr(0);
342
+ * const value2 = pipe(Result.err("error")).map(unwrapWithDefault).value;
343
+ * console.log(value2); // 0
344
+ * ```
345
+ */
346
+ export const unwrapOkOr: UnwrapOkOrFnOverload;
347
+ type UnwrapOkOrFnOverload = {
348
+ <R extends Base, D>(result: R, defaultValue: D): D | UnwrapOk<R>;
349
+ <S, D>(defaultValue: D): <E>(result: Result<S, E>) => D | S;
350
+ };
351
+ /**
352
+ * Unwraps a `Result`, returning the error value.
353
+ * Throws an error if the `Result` is `Result.Ok`.
354
+ *
355
+ * This function is used when you expect a Result to be an error and want to
356
+ * extract the error value. If the Result is unexpectedly Ok, it will throw
357
+ * an error with information about the unexpected success value.
358
+ *
359
+ * @template R The `Result.Base` type to unwrap.
360
+ * @param result The `Result` to unwrap.
361
+ * @param toStr An optional function to convert the success value to a string for the error message when the Result is unexpectedly Ok. Defaults to `String`.
362
+ * @returns The error value if `Result.Err`.
363
+ * @throws {Error} Error with message "Expected Err but got Ok: {value}" if the `Result` is `Result.Ok`.
364
+ * @example
365
+ * ```typescript
366
+ * // Basic usage - extracting error from known failure
367
+ * const failure = Result.err("Network timeout");
368
+ * console.log(Result.unwrapErrThrow(failure)); // "Network timeout"
369
+ *
370
+ * // Throws when Result is unexpectedly Ok
371
+ * const success = Result.ok(42);
372
+ * try {
373
+ * Result.unwrapErrThrow(success); // throws Error: "Expected Err but got Ok: 42"
374
+ * } catch (error) {
375
+ * console.log(error.message); // "Expected Err but got Ok: 42"
376
+ * }
377
+ *
378
+ * // Custom success value string conversion
379
+ * interface User { name: string; id: number; }
380
+ * const userResult = Result.ok<User>({ name: "John", id: 123 });
381
+ * try {
382
+ * Result.unwrapErrThrow(userResult, user => `User(${user.name}:${user.id})`);
383
+ * } catch (error) {
384
+ * console.log(error.message); // "Expected Err but got Ok: User(John:123)"
385
+ * }
386
+ *
387
+ * // In error handling contexts
388
+ * const validateAndGetError = (result: Result<any, ValidationError>) => {
389
+ * if (Result.isErr(result)) {
390
+ * return Result.unwrapErrThrow(result); // Safe to unwrap error
391
+ * }
392
+ * throw new Error("Validation unexpectedly succeeded");
393
+ * };
394
+ * ```
395
+ */
396
+ export const unwrapErrThrow: <R extends Base>(result: R, toStr?: (v: UnwrapOk<R>) => string) => UnwrapErr<R>;
397
+ /**
398
+ * Unwraps a `Result`, returning the error value or `undefined` if it is `Result.Ok`.
399
+ *
400
+ * This provides a safe way to extract error values from Results without throwing
401
+ * exceptions. Useful for error handling patterns where you want to check for
402
+ * specific error conditions.
403
+ *
404
+ * @template R The `Result.Base` type to unwrap.
405
+ * @param result The `Result` to unwrap.
406
+ * @returns The error value if `Result.Err`, otherwise `undefined`.
407
+ * @example
408
+ * ```typescript
409
+ * // Basic error extraction
410
+ * const failure = Result.err("Connection failed");
411
+ * console.log(Result.unwrapErr(failure)); // "Connection failed"
412
+ *
413
+ * const success = Result.ok(42);
414
+ * console.log(Result.unwrapErr(success)); // undefined
415
+ *
416
+ * // Error handling patterns
417
+ * const handleApiCall = (result: Result<Data, ApiError>) => {
418
+ * const error = Result.unwrapErr(result);
419
+ * if (error !== undefined) {
420
+ * switch (error.type) {
421
+ * case "NETWORK_ERROR":
422
+ * return retry(result);
423
+ * case "AUTH_ERROR":
424
+ * return redirectToLogin();
425
+ * default:
426
+ * return showGenericError(error);
427
+ * }
428
+ * }
429
+ * // Handle success case...
430
+ * };
431
+ *
432
+ * // Collecting errors from multiple operations
433
+ * const results = await Promise.all([
434
+ * operation1(),
435
+ * operation2(),
436
+ * operation3()
437
+ * ]);
438
+ *
439
+ * const errors = results
440
+ * .map(Result.unwrapErr)
441
+ * .filter(err => err !== undefined); // Only actual errors
442
+ * ```
443
+ */
444
+ export const unwrapErr: <R extends Base>(result: R) => UnwrapErr<R> | undefined;
445
+ /**
446
+ * Unwraps a `Result`, returning the error value or a default value if it is `Result.Ok`.
447
+ * @template R The `Result.Base` type to unwrap.
448
+ * @template D The type of the default value.
449
+ * @param result The `Result` to unwrap.
450
+ * @param defaultValue The value to return if `result` is `Result.Ok`.
451
+ * @returns The error value if `Result.Err`, otherwise `defaultValue`.
452
+ * @example
453
+ * ```typescript
454
+ * // Regular usage
455
+ * const result = Result.err("failed");
456
+ * const error = Result.unwrapErrOr(result, "default");
457
+ * console.log(error); // "failed"
458
+ *
459
+ * // Curried usage for pipe composition
460
+ * const unwrapErrorWithDefault = Result.unwrapErrOr("unknown error");
461
+ * const error2 = pipe(Result.ok(42)).map(unwrapErrorWithDefault).value;
462
+ * console.log(error2); // "unknown error"
463
+ * ```
464
+ */
465
+ export const unwrapErrOr: UnwrapErrOrFnOverload;
466
+ type UnwrapErrOrFnOverload = {
467
+ <R extends Base, D>(result: R, defaultValue: D): D | UnwrapErr<R>;
468
+ <E, D>(defaultValue: D): <S>(result: Result<S, E>) => D | E;
469
+ };
470
+ /**
471
+ * Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the success value.
472
+ * If the `Result` is `Result.Err`, returns the original `Err`.
473
+ * @template R The input `Result.Base` type.
474
+ * @template S2 The type of the success value returned by the mapping function.
475
+ * @param result The `Result` to map.
476
+ * @param mapFn The function to apply to the success value if present.
477
+ * @returns A new `Result<S2, UnwrapErr<R>>`.
478
+ * @example
479
+ * ```typescript
480
+ * // Regular usage
481
+ * const result = Result.ok(5);
482
+ * const mapped = Result.map(result, x => x * 2);
483
+ * console.log(Result.unwrap(mapped)); // 10
484
+ *
485
+ * // Curried version for use with pipe
486
+ * const doubler = Result.map((x: number) => x * 2);
487
+ * const result2 = pipe(Result.ok(5)).map(doubler).value;
488
+ * console.log(Result.unwrap(result2)); // 10
489
+ * ```
490
+ */
491
+ export const map: MapFnOverload;
492
+ type MapFnOverload = {
493
+ <R extends Base, S2>(result: R, mapFn: (value: UnwrapOk<R>) => S2): Result<S2, UnwrapErr<R>>;
494
+ <S, S2>(mapFn: (value: S) => S2): <E>(result: Result<S, E>) => Result<S2, E>;
495
+ };
496
+ /**
497
+ * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the error value.
498
+ * If the `Result` is `Result.Ok`, returns the original `Ok`.
499
+ * @template R The input `Result.Base` type.
500
+ * @template E2 The type of the error value returned by the mapping function.
501
+ * @param result The `Result` to map.
502
+ * @param mapFn The function to apply to the error value if present.
503
+ * @returns A new `Result<UnwrapOk<R>, E2>`.
504
+ * @example
505
+ * ```typescript
506
+ * // Regular usage
507
+ * const result = Result.err("error");
508
+ * const mapped = Result.mapErr(result, e => e.toUpperCase());
509
+ * console.log(Result.unwrapErr(mapped)); // "ERROR"
510
+ *
511
+ * // Curried usage for pipe composition
512
+ * const errorUppercase = Result.mapErr((e: string) => e.toUpperCase());
513
+ * const result2 = pipe(Result.err("error")).map(errorUppercase).value;
514
+ * console.log(Result.unwrapErr(result2)); // "ERROR"
515
+ * ```
516
+ */
517
+ export const mapErr: MapErrFnOverload;
518
+ type MapErrFnOverload = {
519
+ <R extends Base, E2>(result: R, mapFn: (error: UnwrapErr<R>) => E2): Result<UnwrapOk<R>, E2>;
520
+ <E, E2>(mapFn: (error: E) => E2): <S>(result: Result<S, E>) => Result<S, E2>;
521
+ };
522
+ /**
523
+ * Applies one of two functions depending on whether the `Result` is `Ok` or `Err`.
524
+ * @template R The input `Result.Base` type.
525
+ * @template S2 The type of the success value returned by `mapFn`.
526
+ * @template E2 The type of the error value returned by `mapErrFn`.
527
+ * @param result The `Result` to fold.
528
+ * @param mapFn The function to apply if `result` is `Ok`.
529
+ * @param mapErrFn The function to apply if `result` is `Err`.
530
+ * @returns A new `Result<S2, E2>` based on the applied function.
531
+ * @example
532
+ * ```typescript
533
+ * // Regular usage
534
+ * const result = Result.ok(42);
535
+ * const folded = Result.fold(result, x => x * 2, () => 0);
536
+ * console.log(Result.unwrapOk(folded)); // 84
537
+ *
538
+ * // Curried usage for pipe composition
539
+ * const folder = Result.fold((x: number) => x * 2, () => 0);
540
+ * const result2 = pipe(Result.ok(42)).map(folder).value;
541
+ * console.log(Result.unwrapOk(result2)); // 84
542
+ * ```
543
+ */
544
+ export const fold: FoldFnOverload;
545
+ type FoldFnOverload = {
546
+ <R extends Base, S2, E2>(result: R, mapFn: (value: UnwrapOk<R>) => S2, mapErrFn: (error: UnwrapErr<R>) => E2): Result<S2, E2>;
547
+ <S, E, S2, E2>(mapFn: (value: S) => S2, mapErrFn: (error: E) => E2): (result: Result<S, E>) => Result<S2, E2>;
548
+ };
549
+ /**
550
+ * Applies a function that returns a `Result` to the success value of a `Result`.
551
+ * If the input is `Err`, returns the original `Err`.
552
+ * This is the monadic bind operation for `Result`.
553
+ * @template R The input `Result.Base` type.
554
+ * @template S2 The success type of the `Result` returned by the function.
555
+ * @template E2 The error type of the `Result` returned by the function.
556
+ * @param result The `Result` to flat map.
557
+ * @param flatMapFn The function to apply that returns a `Result`.
558
+ * @returns The result of applying the function, or the original `Err`.
559
+ * @example
560
+ * ```typescript
561
+ * // Regular usage
562
+ * const divide = (a: number, b: number): Result<number, string> =>
563
+ * b === 0 ? Result.err("Division by zero") : Result.ok(a / b);
564
+ *
565
+ * const result = Result.flatMap(Result.ok(10), x => divide(x, 2));
566
+ * console.log(Result.unwrapOk(result)); // 5
567
+ *
568
+ * // Curried usage for pipe composition
569
+ * const divideBy2 = Result.flatMap((x: number) => divide(x, 2));
570
+ * const result2 = pipe(Result.ok(10)).map(divideBy2).value;
571
+ * console.log(Result.unwrapOk(result2)); // 5
572
+ * ```
573
+ */
574
+ export const flatMap: FlatMapFnOverload;
575
+ type FlatMapFnOverload = {
576
+ <R extends Base, S2, E2>(result: R, flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>): Result<S2, E2 | UnwrapErr<R>>;
577
+ <S, S2, E2>(flatMapFn: (value: S) => Result<S2, E2>): <E>(result: Result<S, E>) => Result<S2, E | E2>;
578
+ };
579
+ /**
580
+ * Unwraps a `Result`, returning the success value or throwing an error with the provided message.
581
+ * @template R The `Result.Base` type to unwrap.
582
+ * @param result The `Result` to unwrap.
583
+ * @param message The error message to throw if the `Result` is `Result.Err`.
584
+ * @returns The success value if `Result.Ok`.
585
+ * @throws Error with the provided message if the `Result` is `Result.Err`.
586
+ * @example
587
+ * ```typescript
588
+ * // Regular usage
589
+ * const result = Result.ok(42);
590
+ * const value = Result.expectToBe(result, "Operation must succeed");
591
+ * console.log(value); // 42
592
+ *
593
+ * // Curried usage for pipe composition
594
+ * const mustBeOk = Result.expectToBe("Operation must succeed");
595
+ * const value2 = pipe(Result.ok(42)).map(mustBeOk).value;
596
+ * console.log(value2); // 42
597
+ * ```
598
+ */
599
+ export const expectToBe: ExpectToBeFnOverload;
600
+ type ExpectToBeFnOverload = {
601
+ <R extends Base>(result: R, message: string): UnwrapOk<R>;
602
+ <S>(message: string): <E>(result: Result<S, E>) => S;
603
+ };
604
+ /**
605
+ * @internal
606
+ * Utility type to extract the resolved value type from a Promise.
607
+ * @template P The Promise type.
608
+ * @example
609
+ * UnwrapPromise<Promise<string>> // string
610
+ * UnwrapPromise<Promise<number>> // number
611
+ */
612
+ type UnwrapPromise<P extends Promise<unknown>> = P extends Promise<infer V> ? V : never;
613
+ /**
614
+ * Converts a Promise into a Promise that resolves to a `Result`.
615
+ * If the input Promise resolves, the `Result` will be `Ok` with the resolved value.
616
+ * If the input Promise rejects, the `Result` will be `Err` with the rejection reason.
617
+ * @template P The type of the input Promise.
618
+ * @param promise The Promise to convert.
619
+ * @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`.
620
+ */
621
+ export const fromPromise: <P extends Promise<unknown>>(promise: P) => Promise<Result<UnwrapPromise<P>, unknown>>;
622
+ /**
623
+ * Wraps a function that may throw an exception in a `Result`.
624
+ *
625
+ * This is a fundamental utility for converting traditional exception-based error
626
+ * handling into Result-based error handling. Any thrown value is converted to an
627
+ * Error object for consistent error handling.
628
+ *
629
+ * If the function executes successfully, returns `Result.Ok` with the result.
630
+ * If the function throws, returns `Result.Err` with the caught error.
631
+ *
632
+ * @template T The return type of the function.
633
+ * @param fn The function to execute that may throw.
634
+ * @returns A `Result<T, Error>` containing either the successful result or the caught error.
635
+ * @example
636
+ * ```typescript
637
+ * // Wrapping JSON.parse which can throw
638
+ * const parseJson = <T>(text: string): Result<T, Error> =>
639
+ * Result.fromThrowable(() => JSON.parse(text) as T);
640
+ *
641
+ * const validJson = parseJson<{valid: boolean}>('{"valid": true}');
642
+ * if (Result.isOk(validJson)) {
643
+ * console.log(validJson.value.valid); // true
644
+ * }
645
+ *
646
+ * const invalidJson = parseJson('{invalid json}');
647
+ * if (Result.isErr(invalidJson)) {
648
+ * console.log(invalidJson.value.message); // SyntaxError message
649
+ * }
650
+ *
651
+ * // Using with custom validation
652
+ * const parsePositiveNumber = (str: string): Result<number, Error> =>
653
+ * Result.fromThrowable(() => {
654
+ * const num = Number(str);
655
+ * if (Number.isNaN(num)) throw new Error(`Not a number: ${str}`);
656
+ * if (num <= 0) throw new Error(`Must be positive: ${num}`);
657
+ * return num;
658
+ * });
659
+ *
660
+ * const success = parsePositiveNumber('42');
661
+ * console.log(Result.unwrapOkOr(success, 0)); // 42
662
+ *
663
+ * const failure = parsePositiveNumber('abc');
664
+ * console.log(Result.unwrapOkOr(failure, 0)); // 0
665
+ *
666
+ * // Wrapping DOM operations that might fail
667
+ * const getElementText = (id: string): Result<string, Error> =>
668
+ * Result.fromThrowable(() => {
669
+ * const element = document.getElementById(id);
670
+ * if (!element) throw new Error(`Element not found: ${id}`);
671
+ * return element.textContent || "";
672
+ * });
673
+ *
674
+ * // Wrapping file operations
675
+ * const readFileSync = (path: string): Result<string, Error> =>
676
+ * Result.fromThrowable(() =>
677
+ * require('fs').readFileSync(path, 'utf8')
678
+ * );
679
+ * ```
680
+ */
681
+ export const fromThrowable: <T>(fn: () => T) => Result<T, Error>;
682
+ /**
683
+ * Swaps the success and error values of a `Result`.
684
+ * @template R The input `Result.Base` type.
685
+ * @param result The `Result` to swap.
686
+ * @returns A new `Result` with success and error swapped.
687
+ * @example
688
+ * ```typescript
689
+ * const okResult = Result.ok(42);
690
+ * const swapped = Result.swap(okResult);
691
+ * console.log(Result.isErr(swapped)); // true
692
+ * console.log(Result.unwrapErr(swapped)); // 42
693
+ * ```
694
+ */
695
+ export const swap: <R extends Base>(result: R) => Result<UnwrapErr<R>, UnwrapOk<R>>;
696
+ /**
697
+ * Converts a `Result` to an `Optional`.
698
+ *
699
+ * This conversion is useful when you want to discard error information and only
700
+ * care about whether an operation succeeded. The error information is lost in
701
+ * this conversion, so use it when error details are not needed.
702
+ *
703
+ * If the `Result` is `Ok`, returns `Some` with the value.
704
+ * If the `Result` is `Err`, returns `None`.
705
+ *
706
+ * @template R The input `Result.Base` type.
707
+ * @param result The `Result` to convert.
708
+ * @returns An `Optional<UnwrapOk<R>>` containing the success value or representing `None`.
709
+ * @example
710
+ * ```typescript
711
+ * // Basic conversion
712
+ * const okResult = Result.ok(42);
713
+ * const optional = Result.toOptional(okResult);
714
+ * console.log(Optional.isSome(optional)); // true
715
+ * console.log(Optional.unwrap(optional)); // 42
716
+ *
717
+ * const errResult = Result.err("Network error");
718
+ * const none = Result.toOptional(errResult);
719
+ * console.log(Optional.isNone(none)); // true
720
+ *
721
+ * // Use case: when you only care about success, not error details
722
+ * const fetchUserName = (id: number): Result<string, ApiError> => {
723
+ * // ... implementation
724
+ * };
725
+ *
726
+ * const maybeUserName = Result.toOptional(fetchUserName(123));
727
+ * const displayName = Optional.unwrapOr(maybeUserName, "Unknown User");
728
+ *
729
+ * // Converting multiple Results and filtering successes
730
+ * const userIds = [1, 2, 3, 4];
731
+ * const userNames = userIds
732
+ * .map(fetchUserName)
733
+ * .map(Result.toOptional)
734
+ * .filter(Optional.isSome)
735
+ * .map(Optional.unwrap); // string[]
736
+ *
737
+ * // Chaining with Optional operations
738
+ * const processResult = (r: Result<string, Error>) =>
739
+ * pipe(Result.toOptional(r))
740
+ * .map(Optional.map(s => s.toUpperCase()))
741
+ * .map(Optional.filter(s => s.length > 0))
742
+ * .value;
743
+ * ```
744
+ */
745
+ export const toOptional: <R extends Base>(result: R) => Optional<UnwrapOk<R>>;
746
+ /**
747
+ * Returns the `Result` if it is `Ok`, otherwise returns the alternative.
748
+ * @template R The input `Result.Base` type.
749
+ * @param result The `Result` to check.
750
+ * @param alternative The alternative `Result` to return if the first is `Err`.
751
+ * @returns The first `Result` if `Ok`, otherwise the alternative.
752
+ * @example
753
+ * ```typescript
754
+ * // Regular usage
755
+ * const primary = Result.err("error");
756
+ * const fallback = Result.ok("default");
757
+ * const result = Result.orElse(primary, fallback);
758
+ * console.log(Result.unwrapOk(result)); // "default"
759
+ *
760
+ * // Curried usage for pipe composition
761
+ * const fallbackTo = Result.orElse(Result.ok("fallback"));
762
+ * const result2 = pipe(Result.err("error")).map(fallbackTo).value;
763
+ * console.log(Result.unwrapOk(result2)); // "fallback"
764
+ * ```
765
+ */
766
+ export const orElse: OrElseFnOverload;
767
+ type OrElseFnOverload = {
768
+ <R extends Base, R2 extends Base>(result: R, alternative: R2): NarrowToOk<R> | R2;
769
+ <S, E, S2, E2>(alternative: Result<S2, E2>): (result: Result<S, E>) => Result<S, E> | Result<S2, E2>;
770
+ };
771
+ /**
772
+ * Combines two `Result` values into a single `Result` containing a tuple.
773
+ * If either `Result` is `Err`, returns the first `Err` encountered.
774
+ * @template S1 The success type of the first `Result`.
775
+ * @template E1 The error type of the first `Result`.
776
+ * @template S2 The success type of the second `Result`.
777
+ * @template E2 The error type of the second `Result`.
778
+ * @param resultA The first `Result`.
779
+ * @param resultB The second `Result`.
780
+ * @returns A `Result` containing a tuple of both values, or the first `Err`.
781
+ * @example
782
+ * ```typescript
783
+ * const a = Result.ok(1);
784
+ * const b = Result.ok("hello");
785
+ * const zipped = Result.zip(a, b);
786
+ * console.log(Result.unwrapOk(zipped)); // [1, "hello"]
787
+ *
788
+ * const withErr = Result.zip(a, Result.err("error"));
789
+ * console.log(Result.unwrapErr(withErr)); // "error"
790
+ * ```
791
+ */
792
+ export const zip: <S1, E1, S2, E2>(resultA: Result<S1, E1>, resultB: Result<S2, E2>) => Result<readonly [S1, S2], E1 | E2>;
793
+ export {};
794
+ }
795
+ export {};
796
+ //# sourceMappingURL=result.d.mts.map