@carbonteq/fp 0.9.1 → 0.10.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 (146) hide show
  1. package/README.md +310 -96
  2. package/dist/errors.cjs +1 -0
  3. package/dist/errors.cjs.map +1 -1
  4. package/dist/errors.d.cts.map +1 -1
  5. package/dist/errors.d.mts.map +1 -1
  6. package/dist/errors.mjs.map +1 -1
  7. package/dist/flow-experimental.cjs +286 -0
  8. package/dist/flow-experimental.cjs.map +1 -0
  9. package/dist/flow-experimental.d.cts +69 -0
  10. package/dist/flow-experimental.d.cts.map +1 -0
  11. package/dist/flow-experimental.d.mts +69 -0
  12. package/dist/flow-experimental.d.mts.map +1 -0
  13. package/dist/flow-experimental.mjs +284 -0
  14. package/dist/flow-experimental.mjs.map +1 -0
  15. package/dist/flow.cjs +296 -0
  16. package/dist/flow.cjs.map +1 -0
  17. package/dist/flow.d.cts +79 -0
  18. package/dist/flow.d.cts.map +1 -0
  19. package/dist/flow.d.mts +79 -0
  20. package/dist/flow.d.mts.map +1 -0
  21. package/dist/flow.mjs +294 -0
  22. package/dist/flow.mjs.map +1 -0
  23. package/dist/index.cjs +26 -11
  24. package/dist/index.d.cts +8 -3
  25. package/dist/index.d.mts +8 -3
  26. package/dist/index.mjs +8 -3
  27. package/dist/match.cjs +282 -0
  28. package/dist/match.cjs.map +1 -1
  29. package/dist/match.d.cts +471 -5
  30. package/dist/match.d.cts.map +1 -1
  31. package/dist/match.d.mts +471 -5
  32. package/dist/match.d.mts.map +1 -1
  33. package/dist/match.mjs +277 -1
  34. package/dist/match.mjs.map +1 -1
  35. package/dist/option-experimental.cjs +1503 -0
  36. package/dist/option-experimental.cjs.map +1 -0
  37. package/dist/option-experimental.d.cts +1383 -0
  38. package/dist/option-experimental.d.cts.map +1 -0
  39. package/dist/option-experimental.d.mts +1383 -0
  40. package/dist/option-experimental.d.mts.map +1 -0
  41. package/dist/option-experimental.mjs +1501 -0
  42. package/dist/option-experimental.mjs.map +1 -0
  43. package/dist/option.cjs +556 -29
  44. package/dist/option.cjs.map +1 -1
  45. package/dist/option.d.cts +505 -24
  46. package/dist/option.d.cts.map +1 -1
  47. package/dist/option.d.mts +505 -24
  48. package/dist/option.d.mts.map +1 -1
  49. package/dist/option.mjs +556 -30
  50. package/dist/option.mjs.map +1 -1
  51. package/dist/result-experimental.cjs +1266 -0
  52. package/dist/result-experimental.cjs.map +1 -0
  53. package/dist/result-experimental.d.cts +1127 -0
  54. package/dist/result-experimental.d.cts.map +1 -0
  55. package/dist/result-experimental.d.mts +1127 -0
  56. package/dist/result-experimental.d.mts.map +1 -0
  57. package/dist/result-experimental.mjs +1265 -0
  58. package/dist/result-experimental.mjs.map +1 -0
  59. package/dist/result.cjs +715 -122
  60. package/dist/result.cjs.map +1 -1
  61. package/dist/result.d.cts +785 -94
  62. package/dist/result.d.cts.map +1 -1
  63. package/dist/result.d.mts +785 -94
  64. package/dist/result.d.mts.map +1 -1
  65. package/dist/result.mjs +714 -117
  66. package/dist/result.mjs.map +1 -1
  67. package/dist/tuple.cjs +3 -0
  68. package/dist/tuple.cjs.map +1 -1
  69. package/dist/tuple.d.cts +5 -1
  70. package/dist/tuple.d.cts.map +1 -1
  71. package/dist/tuple.d.mts +5 -1
  72. package/dist/tuple.d.mts.map +1 -1
  73. package/dist/tuple.mjs +2 -0
  74. package/dist/tuple.mjs.map +1 -1
  75. package/dist/unit.cjs +2 -0
  76. package/dist/unit.cjs.map +1 -1
  77. package/dist/unit.d.cts +2 -0
  78. package/dist/unit.d.cts.map +1 -1
  79. package/dist/unit.d.mts +2 -0
  80. package/dist/unit.d.mts.map +1 -1
  81. package/dist/unit.mjs +1 -0
  82. package/dist/unit.mjs.map +1 -1
  83. package/dist/utils.cjs +15 -0
  84. package/dist/utils.cjs.map +1 -1
  85. package/dist/utils.d.cts +10 -1
  86. package/dist/utils.d.cts.map +1 -1
  87. package/dist/utils.d.mts +10 -1
  88. package/dist/utils.d.mts.map +1 -1
  89. package/dist/utils.mjs +13 -1
  90. package/dist/utils.mjs.map +1 -1
  91. package/examples/README.md +46 -0
  92. package/examples/flow/01-basics.ts +74 -0
  93. package/examples/flow/02-async.ts +73 -0
  94. package/examples/flow/03-recovery.ts +60 -0
  95. package/examples/flow/04-complex-backend.ts +237 -0
  96. package/examples/flow/05-complex-backend-asyncGen.ts +224 -0
  97. package/examples/flow/index.ts +13 -0
  98. package/examples/flow-experimental/01-basics.ts +78 -0
  99. package/examples/flow-experimental/02-async.ts +77 -0
  100. package/examples/flow-experimental/03-recovery.ts +93 -0
  101. package/examples/flow-experimental/04-complex-backend.ts +241 -0
  102. package/examples/flow-experimental/05-complex-backend-asyncGen.ts +229 -0
  103. package/examples/flow-experimental/index.ts +14 -0
  104. package/examples/match/01-stable-match.ts +147 -0
  105. package/examples/match/index.ts +19 -0
  106. package/examples/match-experimental/01-experimental-match.ts +150 -0
  107. package/examples/match-experimental/index.ts +18 -0
  108. package/examples/option/01-basics.ts +16 -0
  109. package/examples/option/02-map-flatMap.ts +36 -0
  110. package/examples/option/03-zip-flatZip.ts +23 -0
  111. package/examples/option/04-constructors.ts +21 -0
  112. package/examples/option/05-transform.ts +15 -0
  113. package/examples/option/06-aggregation.ts +14 -0
  114. package/examples/option/index.ts +49 -0
  115. package/examples/option-experimental/01-map.ts +109 -0
  116. package/examples/option-experimental/02-flatMap.ts +234 -0
  117. package/examples/option-experimental/03-zip.ts +197 -0
  118. package/examples/option-experimental/04-flatZip.ts +251 -0
  119. package/examples/option-experimental/05-gen.ts +360 -0
  120. package/examples/option-experimental/06-genAdapter.ts +463 -0
  121. package/examples/option-experimental/07-asyncGen.ts +353 -0
  122. package/examples/option-experimental/08-asyncGenAdapter.ts +514 -0
  123. package/examples/option-experimental/09-workflow-simple-sync.ts +321 -0
  124. package/examples/option-experimental/10-workflow-async.ts +378 -0
  125. package/examples/option-experimental/index.ts +341 -0
  126. package/examples/option-experimental/shared-domain.ts +325 -0
  127. package/examples/result/01-basics.ts +16 -0
  128. package/examples/result/02-map-flatMap.ts +34 -0
  129. package/examples/result/03-zip-flatZip.ts +23 -0
  130. package/examples/result/04-validation.ts +44 -0
  131. package/examples/result/05-constructors.ts +30 -0
  132. package/examples/result/06-utilities.ts +36 -0
  133. package/examples/result/index.ts +53 -0
  134. package/examples/result-experimental/01-map.ts +60 -0
  135. package/examples/result-experimental/02-flatMap.ts +169 -0
  136. package/examples/result-experimental/03-zip.ts +145 -0
  137. package/examples/result-experimental/04-flatZip.ts +252 -0
  138. package/examples/result-experimental/05-gen.ts +293 -0
  139. package/examples/result-experimental/06-genAdapter.ts +396 -0
  140. package/examples/result-experimental/07-asyncGen.ts +379 -0
  141. package/examples/result-experimental/08-asyncGenAdapter.ts +553 -0
  142. package/examples/result-experimental/09-workflow-simple-sync.ts +304 -0
  143. package/examples/result-experimental/10-workflow-async.ts +356 -0
  144. package/examples/result-experimental/index.ts +312 -0
  145. package/examples/result-experimental/shared-domain.ts +263 -0
  146. package/package.json +50 -18
@@ -0,0 +1,1383 @@
1
+ import { UNIT } from "./unit.cjs";
2
+ import { ExperimentalResult } from "./result-experimental.cjs";
3
+
4
+ //#region src/option-experimental.d.ts
5
+ type Predicate<T> = (val: T) => boolean;
6
+ type Falsy = false | 0 | "" | null | undefined;
7
+ /**
8
+ * Error thrown when attempting to unwrap a `None` value.
9
+ *
10
+ * This exception is thrown by {@link ExperimentalOption.unwrap} when called on a `None`.
11
+ * It represents a programmer error - you should only call `unwrap()` when
12
+ * you are certain the ExperimentalOption contains a value, or use safe alternatives
13
+ * like {@link ExperimentalOption.unwrapOr} or {@link ExperimentalOption.safeUnwrap}.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * try {
18
+ * ExperimentalOption.None.unwrap();
19
+ * } catch (e) {
20
+ * e instanceof UnwrappedNone // true
21
+ * e.name // "UnwrapError"
22
+ * }
23
+ * ```
24
+ */
25
+ declare class UnwrappedNone extends Error {
26
+ readonly name = "UnwrapError";
27
+ constructor();
28
+ }
29
+ /** ExperimentalOption alias for presence-without-payload values. */
30
+ type UnitOption = ExperimentalOption<UNIT>;
31
+ /** Extracts the contained value type from an ExperimentalOption type. */
32
+ type UnwrapOption<T> = T extends ExperimentalOption<infer R> ? R : never;
33
+ type CombinedOptions<T extends ExperimentalOption<unknown>[]> = { [K in keyof T]: UnwrapOption<T[K]> };
34
+ interface MatchCases<T, U> {
35
+ Some: (val: T) => U;
36
+ None: () => U;
37
+ }
38
+ /**
39
+ * Wrapper that makes ExperimentalOption yieldable with proper type tracking.
40
+ *
41
+ * @internal
42
+ */
43
+ declare class OptionYieldWrap<T> {
44
+ readonly option: ExperimentalOption<T>;
45
+ constructor(option: ExperimentalOption<T>);
46
+ [Symbol.iterator](): Generator<OptionYieldWrap<T>, T, unknown>;
47
+ }
48
+ /**
49
+ * Wrapper that makes ExperimentalOption yieldable in async generators with proper type tracking.
50
+ *
51
+ * @internal
52
+ */
53
+ declare class AsyncOptionYieldWrap<T> {
54
+ readonly option: Promise<ExperimentalOption<T>>;
55
+ constructor(option: ExperimentalOption<T> | Promise<ExperimentalOption<T>>);
56
+ [Symbol.asyncIterator](): AsyncGenerator<AsyncOptionYieldWrap<T>, Awaited<T>, unknown>;
57
+ }
58
+ /**
59
+ * A type-safe container for optional values.
60
+ *
61
+ * `ExperimentalOption<T>` represents a value that may or may not be present. It eliminates the
62
+ * need for `null` and `undefined` checks by making absence explicit through the
63
+ * type system.
64
+ *
65
+ * ## States
66
+ *
67
+ * - **Some\<T\>**: Contains a value of type `T`
68
+ * - **None**: Represents absence of a value (singleton)
69
+ *
70
+ * ## Basic Usage
71
+ *
72
+ * ```ts
73
+ * import { ExperimentalOption } from "@carbonteq/fp";
74
+ *
75
+ * // Creating ExperimentalOptions
76
+ * const some: ExperimentalOption<number> = ExperimentalOption.Some(42);
77
+ * const none: ExperimentalOption<number> = ExperimentalOption.None;
78
+ *
79
+ * // From nullable values
80
+ * const fromNull = ExperimentalOption.fromNullable(null); // None
81
+ * const fromValue = ExperimentalOption.fromNullable("hello"); // Some("hello")
82
+ *
83
+ * // Checking and extracting
84
+ * if (some.isSome()) {
85
+ * console.log(some.unwrap()); // 42
86
+ * }
87
+ *
88
+ * // Chaining operations
89
+ * const result = ExperimentalOption.Some(5)
90
+ * .map(x => x * 2)
91
+ * .flatMap(x => ExperimentalOption.Some(x + 1));
92
+ * // Some(11)
93
+ * ```
94
+ *
95
+ * ## Async Variants
96
+ *
97
+ * For operations that return Promises, use `*Async` methods:
98
+ *
99
+ * ```ts
100
+ * // Sync
101
+ * ExperimentalOption.Some(5).map(x => x * 2); // ExperimentalOption<number>
102
+ *
103
+ * // Async
104
+ * ExperimentalOption.Some(5).mapAsync(async x => x * 2); // Promise<ExperimentalOption<number>>
105
+ * ```
106
+ *
107
+ * @template T - The type of the contained value
108
+ *
109
+ * @see {@link https://carbonteq.github.io/ct-fp/option | ExperimentalOption Documentation}
110
+ */
111
+ declare class ExperimentalOption<T> {
112
+ #private;
113
+ /** Discriminant tag for type-level identification */
114
+ readonly _tag: "Some" | "None";
115
+ private constructor();
116
+ /**
117
+ * Singleton `None` instance representing absence of a value.
118
+ *
119
+ * This is a shared instance - all `None` values reference the same object.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * const none1 = ExperimentalOption.None;
124
+ * const none2 = ExperimentalOption.None;
125
+ * none1 === none2 // true
126
+ * ```
127
+ */
128
+ static readonly None: ExperimentalOption<never>;
129
+ /**
130
+ * Creates a `Some` variant containing the provided value.
131
+ *
132
+ * @template Inner - The type of value to wrap
133
+ * @param val - The value to wrap in a Some
134
+ * @returns An `ExperimentalOption<Inner>` in the Some state
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const opt = ExperimentalOption.Some(42); // ExperimentalOption<number>
139
+ * const str = ExperimentalOption.Some("hello"); // ExperimentalOption<string>
140
+ * const obj = ExperimentalOption.Some({ id: 1 }); // ExperimentalOption<{ id: number }>
141
+ * ```
142
+ */
143
+ static Some<Inner>(this: void, val: Inner): ExperimentalOption<Inner>;
144
+ /**
145
+ * Creates an `ExperimentalOption` from a nullable value, returning `None` for `null` or `undefined`.
146
+ *
147
+ * Unlike {@link fromFalsy}, this only treats `null` and `undefined` as absence.
148
+ * Values like `0`, `""`, and `false` are considered valid and wrapped in `Some`.
149
+ *
150
+ * @template T - The input type which may include null/undefined
151
+ * @param val - The value to convert to an ExperimentalOption
152
+ * @returns `Some<NonNullable<T>>` if value is non-nullish, else `None`
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * ExperimentalOption.fromNullable("hello") // Some("hello")
157
+ * ExperimentalOption.fromNullable(null) // None
158
+ * ExperimentalOption.fromNullable(undefined) // None
159
+ * ExperimentalOption.fromNullable(0) // Some(0) - 0 is not nullish
160
+ * ExperimentalOption.fromNullable("") // Some("") - empty string is not nullish
161
+ * ExperimentalOption.fromNullable(false) // Some(false) - false is not nullish
162
+ *
163
+ * // With user input
164
+ * function findUser(id: string): User | null {
165
+ * return database.getUser(id) ?? null;
166
+ * }
167
+ * const user = ExperimentalOption.fromNullable(findUser("123"));
168
+ * // ExperimentalOption<User>
169
+ * ```
170
+ */
171
+ static fromNullable<T>(val: T): ExperimentalOption<NonNullable<T>>;
172
+ /**
173
+ * Creates an `ExperimentalOption` from a value that may be falsy, returning `None` for falsy values.
174
+ *
175
+ * Treats `false`, `0`, `""`, `null`, and `undefined` as absence.
176
+ * Use {@link fromNullable} if you only want to exclude `null`/`undefined`.
177
+ *
178
+ * @template T - The type of value to wrap
179
+ * @param val - The value to convert to an ExperimentalOption
180
+ * @returns `Some<T>` if value is truthy, else `None`
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * ExperimentalOption.fromFalsy("hello") // Some("hello")
185
+ * ExperimentalOption.fromFalsy("") // None
186
+ * ExperimentalOption.fromFalsy(0) // None
187
+ * ExperimentalOption.fromFalsy(false) // None
188
+ * ExperimentalOption.fromFalsy(null) // None
189
+ * ExperimentalOption.fromFalsy(undefined) // None
190
+ *
191
+ * // Useful for validation
192
+ * const isValid = ExperimentalOption.fromFalsy(getConfigValue("API_KEY"));
193
+ * ```
194
+ */
195
+ static fromFalsy<T>(val: T | Falsy): ExperimentalOption<T>;
196
+ /**
197
+ * Creates an `ExperimentalOption` based on a predicate function result.
198
+ *
199
+ * Returns `Some` if the predicate returns `true`, otherwise `None`.
200
+ *
201
+ * @template T - The type of value to test
202
+ * @param val - The value to test
203
+ * @param pred - A predicate function that returns true if value should be wrapped
204
+ * @returns `Some<T>` if predicate returns true, else `None`
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * const age = 25;
209
+ * ExperimentalOption.fromPredicate(age, a => a >= 18) // Some(25)
210
+ * ExperimentalOption.fromPredicate(15, a => a >= 18) // None
211
+ *
212
+ * // Can be used for validation
213
+ * const isValidEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
214
+ * ExperimentalOption.fromPredicate(user.email, isValidEmail)
215
+ *
216
+ * // With complex predicates
217
+ * const isValidPassword = (pw: string) =>
218
+ * pw.length >= 8 && /[A-Z]/.test(pw) && /[0-9]/.test(pw);
219
+ * ExperimentalOption.fromPredicate(password, isValidPassword)
220
+ * ```
221
+ */
222
+ static fromPredicate<T>(val: T, pred: Predicate<T>): ExperimentalOption<T>;
223
+ /**
224
+ * Combines multiple ExperimentalOptions into a single ExperimentalOption of an array.
225
+ *
226
+ * Returns `Some` containing an array of all unwrapped values if ALL inputs are `Some`.
227
+ * Returns `None` if ANY input is `None`.
228
+ *
229
+ * @template T - Variadic tuple of ExperimentalOption types
230
+ * @param options - Variable number of ExperimentalOptions to combine
231
+ * @returns `ExperimentalOption<[T1, T2, ...]>` with array of values, or `None`
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * // All Some -> array of values
236
+ * const all = ExperimentalOption.all(
237
+ * ExperimentalOption.Some(1),
238
+ * ExperimentalOption.Some(2),
239
+ * ExperimentalOption.Some(3)
240
+ * );
241
+ * // Some([1, 2, 3])
242
+ *
243
+ * // Any None -> None
244
+ * const withNone = ExperimentalOption.all(
245
+ * ExperimentalOption.Some(1),
246
+ * ExperimentalOption.None,
247
+ * ExperimentalOption.Some(3)
248
+ * );
249
+ * // None
250
+ *
251
+ * // Empty -> Some([])
252
+ * const empty = ExperimentalOption.all();
253
+ * // Some([])
254
+ *
255
+ * // Practical example: combine multiple validations
256
+ * const name = ExperimentalOption.fromNullable(user.name);
257
+ * const email = ExperimentalOption.fromNullable(user.email);
258
+ * const age = ExperimentalOption.fromNullable(user.age);
259
+ *
260
+ * const complete = ExperimentalOption.all(name, email, age);
261
+ * // ExperimentalOption<[string, string, number]>
262
+ * ```
263
+ */
264
+ static all<T extends ExperimentalOption<unknown>[]>(...options: T): ExperimentalOption<CombinedOptions<T>>;
265
+ /**
266
+ * Returns the first `Some` from a list of ExperimentalOptions, or `None` if all are `None`.
267
+ *
268
+ * Short-circuits on the first `Some` found, evaluating subsequent options lazily.
269
+ *
270
+ * @template T - The type of values in the ExperimentalOptions
271
+ * @param options - Variable number of ExperimentalOptions to check
272
+ * @returns The first `Some<T>` found, or `None`
273
+ *
274
+ * @example
275
+ * ```ts
276
+ * // Returns first Some
277
+ * const first = ExperimentalOption.any(
278
+ * ExperimentalOption.None,
279
+ * ExperimentalOption.Some("First value"),
280
+ * ExperimentalOption.Some("Second value")
281
+ * );
282
+ * // Some("First value")
283
+ *
284
+ * // All None -> None
285
+ * const allNone = ExperimentalOption.any(
286
+ * ExperimentalOption.None,
287
+ * ExperimentalOption.None,
288
+ * ExperimentalOption.None
289
+ * );
290
+ * // None
291
+ *
292
+ * // Practical: Try multiple fallback sources
293
+ * const config = ExperimentalOption.any(
294
+ * ExperimentalOption.fromNullable(process.env.API_KEY),
295
+ * ExperimentalOption.fromNullable(getEnvVar("API_KEY")),
296
+ * ExperimentalOption.fromNullable(readConfigFile("API_KEY"))
297
+ * );
298
+ *
299
+ * // Empty -> None
300
+ * const empty = ExperimentalOption.any();
301
+ * // None
302
+ * ```
303
+ */
304
+ static any<T>(...options: ExperimentalOption<T>[]): ExperimentalOption<T>;
305
+ /**
306
+ * Type guard that narrows the type to `Some`.
307
+ *
308
+ * Returns `true` if this ExperimentalOption is in the `Some` state, containing a value.
309
+ *
310
+ * @returns `true` if this is a `Some`, `false` if `None`
311
+ *
312
+ * @example
313
+ * ```ts
314
+ * const opt: ExperimentalOption<number> = ExperimentalOption.Some(42);
315
+ *
316
+ * if (opt.isSome()) {
317
+ * // TypeScript knows opt is ExperimentalOption<number> & { _tag: "Some" }
318
+ * console.log(opt.unwrap()); // 42
319
+ * }
320
+ * ```
321
+ */
322
+ isSome(): this is ExperimentalOption<T> & {
323
+ readonly _tag: "Some";
324
+ };
325
+ /**
326
+ * Type guard that narrows the type to `None`.
327
+ *
328
+ * Returns `true` if this ExperimentalOption is in the `None` state (absence of value).
329
+ *
330
+ * @returns `true` if this is `None`, `false` if `Some`
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * const opt: ExperimentalOption<number> = ExperimentalOption.None;
335
+ *
336
+ * if (opt.isNone()) {
337
+ * // TypeScript knows opt is ExperimentalOption<never> & { _tag: "None" }
338
+ * console.log("No value present");
339
+ * }
340
+ * ```
341
+ */
342
+ isNone(): this is ExperimentalOption<never> & {
343
+ readonly _tag: "None";
344
+ };
345
+ /**
346
+ * Type guard that checks if the contained value is the `Unit` type.
347
+ *
348
+ * Returns `true` if this ExperimentalOption contains the special `UNIT` value,
349
+ * used to represent operations that produce no meaningful output.
350
+ *
351
+ * @returns `true` if this contains `UNIT`, `false` otherwise
352
+ *
353
+ * @example
354
+ * ```ts
355
+ * const unitOpt: ExperimentalOption<UNIT> = ExperimentalOption.Some(UNIT);
356
+ * unitOpt.isUnit(); // true
357
+ *
358
+ * const normalOpt: ExperimentalOption<number> = ExperimentalOption.Some(42);
359
+ * normalOpt.isUnit(); // false
360
+ * ```
361
+ */
362
+ isUnit(): this is ExperimentalOption<UNIT>;
363
+ /**
364
+ * Returns the contained value or throws {@link UnwrappedNone} if `None`.
365
+ *
366
+ * This is an unsafe operation that throws on `None`. Use only when you are
367
+ * certain the ExperimentalOption contains a value, or use safe alternatives like
368
+ * {@link unwrapOr} or {@link safeUnwrap}.
369
+ *
370
+ * @throws {@link UnwrappedNone} if this is `None`
371
+ * @returns The contained value
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * ExperimentalOption.Some(42).unwrap(); // 42
376
+ * ExperimentalOption.None.unwrap(); // throws UnwrappedNone
377
+ *
378
+ * // Safe usage pattern
379
+ * const opt = ExperimentalOption.Some(42);
380
+ * if (opt.isSome()) {
381
+ * const value = opt.unwrap(); // Safe: we checked isSome()
382
+ * }
383
+ * ```
384
+ *
385
+ * @see {@link unwrapOr} for safe unwrapping with a default value
386
+ * @see {@link unwrapOrElse} for lazy default computation
387
+ * @see {@link safeUnwrap} for null-returning safe unwrap
388
+ */
389
+ unwrap(): T;
390
+ /**
391
+ * Returns the contained value, or the provided default value if `None`.
392
+ *
393
+ * A safe alternative to {@link unwrap} that doesn't throw.
394
+ *
395
+ * @param defaultValue - The value to return if this is `None`
396
+ * @returns The contained value, or the default
397
+ *
398
+ * @example
399
+ * ```ts
400
+ * ExperimentalOption.Some(42).unwrapOr(0); // 42
401
+ * ExperimentalOption.None.unwrapOr(0); // 0
402
+ *
403
+ * // With user input
404
+ * const age = ExperimentalOption.fromNullable(user.age).unwrapOr(18);
405
+ *
406
+ * // With complex defaults
407
+ * const config = ExperimentalOption.fromNullable(getConfig())
408
+ * .unwrapOr({ port: 3000, host: "localhost" });
409
+ * ```
410
+ *
411
+ * @see {@link unwrapOrElse} for lazy default computation
412
+ * @see {@link safeUnwrap} for null-returning safe unwrap
413
+ */
414
+ unwrapOr(defaultValue: T): T;
415
+ /**
416
+ * Returns the contained value, or computes a default via a function if `None`.
417
+ *
418
+ * Unlike {@link unwrapOr}, the default is computed lazily - only if this is `None`.
419
+ * Use this when the default value is expensive to compute.
420
+ *
421
+ * @param fn - A factory function that produces the default value
422
+ * @returns The contained value, or the computed default
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * // Lazy default - only computed if needed
427
+ * const value = ExperimentalOption.None.unwrapOrElse(() => {
428
+ * console.log("Computing expensive default...");
429
+ * return expensivelyComputeDefault();
430
+ * });
431
+ * // Prints "Computing expensive default..."
432
+ *
433
+ * // Not computed when Some
434
+ * const some = ExperimentalOption.Some(42).unwrapOrElse(() => {
435
+ * console.log("This won't print");
436
+ * return expensivelyComputeDefault();
437
+ * });
438
+ * // Returns 42 without printing
439
+ *
440
+ * // Practical: cache lookup with fallback
441
+ * const cached = ExperimentalOption.fromNullable(cache.get(key))
442
+ * .unwrapOrElse(() => {
443
+ * const value = fetchFromDatabase(key);
444
+ * cache.set(key, value);
445
+ * return value;
446
+ * });
447
+ * ```
448
+ *
449
+ * @see {@link unwrapOr} for simple default values
450
+ * @see {@link unwrap} for unsafe unwrapping
451
+ */
452
+ unwrapOrElse(fn: () => T): T;
453
+ /**
454
+ * Returns the contained value, or `null` if `None`.
455
+ *
456
+ * Provides a safe way to extract values that integrates with TypeScript's
457
+ * null checking while maintaining the ExperimentalOption semantics.
458
+ *
459
+ * @returns The contained value, or `null`
460
+ *
461
+ * @example
462
+ * ```ts
463
+ * ExperimentalOption.Some(42).safeUnwrap(); // 42
464
+ * ExperimentalOption.None.safeUnwrap(); // null
465
+ *
466
+ * // Integration with existing null-handling code
467
+ * const value = ExperimentalOption.fromNullable(user.name).safeUnwrap();
468
+ * if (value !== null) {
469
+ * console.log(value.toUpperCase());
470
+ * }
471
+ *
472
+ * // With nullish coalescing
473
+ * const displayName = ExperimentalOption.fromNullable(user.displayName)
474
+ * .safeUnwrap() ?? user.email;
475
+ * ```
476
+ *
477
+ * @see {@link unwrapOr} for typed defaults
478
+ * @see {@link unwrapOrElse} for lazy defaults
479
+ */
480
+ safeUnwrap(): T | null;
481
+ /**
482
+ * Pattern matches on both states of the ExperimentalOption, providing exhaustive handling.
483
+ *
484
+ * This is the most explicit way to handle ExperimentalOptions, ensuring both `Some` and `None`
485
+ * cases are handled at compile time.
486
+ *
487
+ * @template U - The result type of both branches
488
+ * @param cases - Object containing handlers for both states
489
+ * @returns The result of calling the appropriate handler
490
+ *
491
+ * @example
492
+ * ```ts
493
+ * const result = ExperimentalOption.Some(42).match({
494
+ * Some: (value) => `Got: ${value}`,
495
+ * None: () => "Got nothing"
496
+ * });
497
+ * // "Got: 42"
498
+ *
499
+ * const noneResult = ExperimentalOption.None.match({
500
+ * Some: (value) => `Got: ${value}`,
501
+ * None: () => "Got nothing"
502
+ * });
503
+ * // "Got nothing"
504
+ *
505
+ * // Practical: user lookup
506
+ * const message = findUser(userId).match({
507
+ * Some: (user) => `Welcome, ${user.name}!`,
508
+ * None: () => "User not found"
509
+ * });
510
+ * ```
511
+ *
512
+ * @see {@link isSome} and {@link isNone} for conditional handling
513
+ */
514
+ match<U>(cases: MatchCases<T, U>): U;
515
+ /**
516
+ * Pattern matches using positional arguments (FP convention alias for `match`).
517
+ *
518
+ * This is a more concise alternative to `match` using positional arguments
519
+ * instead of an object. Familiar to developers from Scala, Haskell, and other FP languages.
520
+ *
521
+ * @template U - The result type of both branches
522
+ * @param onSome - Handler called with the value if `Some`
523
+ * @param onNone - Handler called if `None`
524
+ * @returns The result of calling the appropriate handler
525
+ *
526
+ * @example
527
+ * ```ts
528
+ * const result = ExperimentalOption.Some(42).fold(
529
+ * (value) => `Got: ${value}`,
530
+ * () => "Got nothing"
531
+ * );
532
+ * // "Got: 42"
533
+ *
534
+ * const noneResult = ExperimentalOption.None.fold(
535
+ * (value) => `Got: ${value}`,
536
+ * () => "Got nothing"
537
+ * );
538
+ * // "Got nothing"
539
+ *
540
+ * // Equivalent to match:
541
+ * experimentalOption.fold(onSome, onNone);
542
+ * experimentalOption.match({ Some: onSome, None: onNone });
543
+ * ```
544
+ *
545
+ * @see {@link match} for object-based pattern matching
546
+ * @see {@link foldAsync} for async handlers
547
+ */
548
+ fold<U>(onSome: (val: T) => U, onNone: () => U): U;
549
+ /**
550
+ * Async pattern matching using positional arguments.
551
+ *
552
+ * Async variant of `fold` for when handlers return Promises.
553
+ * Rejections from `onSome` are propagated (not converted to `None`).
554
+ *
555
+ * @template U - The result type of both branches
556
+ * @param onSome - Async handler called with the value if `Some`
557
+ * @param onNone - Async handler called if `None`
558
+ * @returns Promise resolving to the result of the appropriate handler
559
+ *
560
+ * @example
561
+ * ```ts
562
+ * const result = await ExperimentalOption.Some(userId).foldAsync(
563
+ * async (id) => await fetchUser(id),
564
+ * async () => await getDefaultUser()
565
+ * );
566
+ *
567
+ * // With database lookup
568
+ * const user = await ExperimentalOption.fromNullable(sessionUserId).foldAsync(
569
+ * async (id) => await db.users.findById(id),
570
+ * async () => ({ id: 0, name: "Guest" })
571
+ * );
572
+ * ```
573
+ *
574
+ * @see {@link fold} for synchronous handlers
575
+ * @see {@link matchAsync} for object-based async pattern matching
576
+ */
577
+ foldAsync<U>(onSome: (val: T) => Promise<U>, onNone: () => Promise<U>): Promise<U>;
578
+ /**
579
+ * Async pattern matching on both states of the ExperimentalOption.
580
+ *
581
+ * Async variant of `match` for when handlers return Promises.
582
+ * Rejections from `Some` handler are propagated (not converted to `None`).
583
+ *
584
+ * @template U - The result type of both branches
585
+ * @param cases - Object containing async handlers for both states
586
+ * @returns Promise resolving to the result of the appropriate handler
587
+ *
588
+ * @example
589
+ * ```ts
590
+ * const result = await ExperimentalOption.Some(userId).matchAsync({
591
+ * Some: async (id) => await fetchUser(id),
592
+ * None: async () => await getDefaultUser()
593
+ * });
594
+ *
595
+ * // With API calls
596
+ * const data = await maybeConfig.matchAsync({
597
+ * Some: async (config) => await loadWithConfig(config),
598
+ * None: async () => await loadDefaults()
599
+ * });
600
+ * ```
601
+ *
602
+ * @see {@link match} for synchronous pattern matching
603
+ * @see {@link foldAsync} for positional-argument async matching
604
+ */
605
+ matchAsync<U>(cases: {
606
+ Some: (val: T) => Promise<U>;
607
+ None: () => Promise<U>;
608
+ }): Promise<U>;
609
+ /**
610
+ * Pattern matches with a subset of cases, using a default for unhandled cases.
611
+ *
612
+ * Unlike `match` which requires all cases, `matchPartial` allows handling
613
+ * only specific cases with a fallback default function.
614
+ *
615
+ * @template U - The result type
616
+ * @param cases - Partial object with optional `Some` and `None` handlers
617
+ * @param getDefault - Lazy default function for unhandled cases
618
+ * @returns The result of the matching handler or the default
619
+ *
620
+ * @example
621
+ * ```ts
622
+ * // Only handle Some, default for None
623
+ * ExperimentalOption.Some(42).matchPartial({ Some: (v) => v * 2 }, () => 0); // 84
624
+ * ExperimentalOption.None.matchPartial({ Some: (v) => v * 2 }, () => 0); // 0
625
+ *
626
+ * // Only handle None
627
+ * ExperimentalOption.Some(42).matchPartial({ None: () => -1 }, () => 100); // 100
628
+ * ExperimentalOption.None.matchPartial({ None: () => -1 }, () => 100); // -1
629
+ *
630
+ * // Lazy default with function
631
+ * experimentalOption.matchPartial(
632
+ * { Some: (v) => process(v) },
633
+ * () => computeExpensiveDefault()
634
+ * );
635
+ * ```
636
+ *
637
+ * @see {@link match} for exhaustive pattern matching
638
+ */
639
+ matchPartial<U>(cases: {
640
+ Some?: (val: T) => NoInfer<U>;
641
+ None?: () => NoInfer<U>;
642
+ }, getDefault: () => U): U;
643
+ /**
644
+ * Transforms the contained value using the provided function.
645
+ *
646
+ * If `Some`, applies the function and wraps the result in a new `Some`.
647
+ * If `None`, returns `None` without calling the function.
648
+ *
649
+ * This method rejects async mappers at compile time - use {@link mapAsync} for those.
650
+ *
651
+ * @template U - The type of the transformed value
652
+ * @param fn - A function that transforms the contained value
653
+ * @returns `Some<U>` with the transformed value, or `None`
654
+ *
655
+ * @example
656
+ * ```ts
657
+ * ExperimentalOption.Some(5).map(x => x * 2); // Some(10)
658
+ * ExperimentalOption.None.map(x => x * 2); // None
659
+ *
660
+ * // Chaining transforms
661
+ * ExperimentalOption.Some("hello")
662
+ * .map(s => s.toUpperCase())
663
+ * .map(s => `${s}!`)
664
+ * .map(s => s.length);
665
+ * // Some(6)
666
+ *
667
+ * // Type transformation
668
+ * ExperimentalOption.Some("123")
669
+ * .map(s => parseInt(s, 10));
670
+ * // Some(123)
671
+ * ```
672
+ *
673
+ * @see {@link mapAsync} for async transformations
674
+ * @see {@link flatMap} for chaining operations that return ExperimentalOptions
675
+ * @see {@link mapOr} for unwrapping with transformation
676
+ */
677
+ map<U>(fn: (val: T) => Promise<U>): never;
678
+ map<U>(fn: (val: T) => U): ExperimentalOption<U>;
679
+ /**
680
+ * Transforms the contained value using an async function.
681
+ *
682
+ * If `Some`, awaits the async function and wraps the result in a new `Some`.
683
+ * If `None`, returns a resolved `None` Promise without calling the function.
684
+ *
685
+ * @template U - The type of the transformed value
686
+ * @param fn - An async function that transforms the contained value
687
+ * @returns `Promise<Some<U>>` with the transformed value, or `Promise<None>`
688
+ *
689
+ * @example
690
+ * ```ts
691
+ * // Simple async transform
692
+ * const result = await ExperimentalOption.Some(5).mapAsync(async x => {
693
+ * return x * 2;
694
+ * });
695
+ * // Some(10)
696
+ *
697
+ * // Chaining with .then()
698
+ * ExperimentalOption.Some(5)
699
+ * .map(x => x * 2) // Some(10)
700
+ * .mapAsync(async x => {
701
+ * return await fetchData(x); // Some<Data>
702
+ * })
703
+ * .then(o => o.map(d => d.name)); // Promise<Some<string>>
704
+ *
705
+ * // Practical: API call after validation
706
+ * const userData = await ExperimentalOption.Some(userId)
707
+ * .mapAsync(async id => await fetchUser(id))
708
+ * .then(o => o.mapAsync(async user => await fetchProfile(user.id)));
709
+ * // Promise<ExperimentalOption<Profile>>
710
+ * ```
711
+ *
712
+ * @see {@link map} for synchronous transformations
713
+ */
714
+ mapAsync<U>(fn: (val: T) => Promise<U>): Promise<ExperimentalOption<U>>;
715
+ /**
716
+ * Maps the value or returns a default value (unwrapped).
717
+ *
718
+ * Unlike {@link map}, this returns the value directly (not wrapped in ExperimentalOption).
719
+ * If `Some`, applies the function and returns the result.
720
+ * If `None`, returns the default value without calling the function.
721
+ *
722
+ * @template U - The type of the result
723
+ * @param defaultValue - The value to return if `None`
724
+ * @param fn - A function to apply if `Some`
725
+ * @returns The mapped value or the default
726
+ *
727
+ * @example
728
+ * ```ts
729
+ * ExperimentalOption.Some(5).mapOr(0, x => x * 2); // 10
730
+ * ExperimentalOption.None.mapOr(0, x => x * 2); // 0
731
+ *
732
+ * // Practical: formatting with fallback
733
+ * const displayName = ExperimentalOption.fromNullable(user.name)
734
+ * .mapOr("Anonymous", name => name.toUpperCase());
735
+ * // "JOHN" or "Anonymous"
736
+ *
737
+ * // With complex defaults
738
+ * const balance = ExperimentalOption.fromNullable(account?.balance)
739
+ * .mapOr(0, b => b.toFixed(2));
740
+ * ```
741
+ *
742
+ * @see {@link map} for wrapped results
743
+ * @see {@link mapOrAsync} for async transformations
744
+ */
745
+ mapOr<U>(defaultValue: U, fn: (val: T) => U): U;
746
+ /**
747
+ * Maps the value using an async function or returns a default value.
748
+ *
749
+ * @template U - The type of the result
750
+ * @param defaultValue - The value to return if `None`
751
+ * @param fn - An async function to apply if `Some`
752
+ * @returns `Promise<U>` with the mapped value or the default
753
+ *
754
+ * @example
755
+ * ```ts
756
+ * const result = await ExperimentalOption.Some(5).mapOrAsync(0, async x => {
757
+ * return x * 2;
758
+ * });
759
+ * // 10
760
+ *
761
+ * const noneResult = await ExperimentalOption.None.mapOrAsync(0, async x => {
762
+ * return x * 2;
763
+ * });
764
+ * // 0
765
+ *
766
+ * // Practical: async fetch with fallback
767
+ * const balance = await ExperimentalOption.fromNullable(userId)
768
+ * .mapOrAsync(0, async id => await getAccountBalance(id));
769
+ * ```
770
+ *
771
+ * @see {@link mapOr} for synchronous transformations
772
+ */
773
+ mapOrAsync<U>(defaultValue: U, fn: (val: T) => Promise<U>): Promise<U>;
774
+ /**
775
+ * Chains operations that return ExperimentalOptions, flattening nested ExperimentalOptions.
776
+ *
777
+ * Also known as `bind` or `chain`. Use this to sequence operations where
778
+ * each step depends on the previous value and may fail.
779
+ *
780
+ * If `Some`, applies the function and returns its result directly.
781
+ * If `None`, returns `None` without calling the function.
782
+ *
783
+ * @template U - The type of the value in the returned ExperimentalOption
784
+ * @param fn - A function that returns an ExperimentalOption
785
+ * @returns The result of the function, or `None`
786
+ *
787
+ * @example
788
+ * ```ts
789
+ * // Basic chaining
790
+ * ExperimentalOption.Some(5).flatMap(x => ExperimentalOption.Some(x + 1)); // Some(6)
791
+ * ExperimentalOption.Some(5).flatMap(x => ExperimentalOption.None); // None
792
+ * ExperimentalOption.None.flatMap(x => ExperimentalOption.Some(x + 1)); // None
793
+ *
794
+ * // Chaining optional operations
795
+ * const email = findUser(userId)
796
+ * .flatMap(user => ExperimentalOption.fromNullable(user.profile))
797
+ * .flatMap(profile => ExperimentalOption.fromNullable(profile.avatar))
798
+ * .flatMap(avatar => ExperimentalOption.fromNullable(avatar.url));
799
+ *
800
+ * // Practical: multi-step validation
801
+ * const valid = ExperimentalOption.Some(input)
802
+ * .flatMap(s => parseEmail(s))
803
+ * .flatMap(email => checkDomain(email))
804
+ * .flatMap(domain => resolveDomain(domain));
805
+ * ```
806
+ *
807
+ * @see {@link flatMapAsync} for async operations
808
+ * @see {@link map} for simple transformations
809
+ * @see {@link gen} for imperative-style chaining
810
+ */
811
+ flatMap<U>(fn: (val: T) => ExperimentalOption<U>): ExperimentalOption<U>;
812
+ /**
813
+ * Chains async operations that return ExperimentalOptions.
814
+ *
815
+ * @template U - The type of the value in the returned ExperimentalOption
816
+ * @param fn - An async function that returns an ExperimentalOption
817
+ * @returns `Promise<ExperimentalOption<U>>` with the result, or `Promise<None>`
818
+ *
819
+ * @example
820
+ * ```ts
821
+ * // Async database lookup chain
822
+ * const profile = await ExperimentalOption.Some(userId)
823
+ * .flatMapAsync(async id => await findUser(id))
824
+ * .flatMapAsync(async user => await getProfile(user.id))
825
+ * .flatMapAsync(async p => await getAvatar(p.avatarId));
826
+ * // Promise<ExperimentalOption<Avatar>>
827
+ *
828
+ * // Practical: multi-step async pipeline
829
+ * const result = await ExperimentalOption.Some(email)
830
+ * .flatMapAsync(validateEmail)
831
+ * .flatMapAsync(e => fetchUserByEmail(e))
832
+ * .flatMapAsync(u => getUserPermissions(u.id));
833
+ * ```
834
+ *
835
+ * @see {@link flatMap} for synchronous operations
836
+ * @see {@link asyncGen} for imperative-style async chaining
837
+ */
838
+ flatMapAsync<U>(fn: (val: T) => Promise<ExperimentalOption<U>>): Promise<ExperimentalOption<U>>;
839
+ /**
840
+ * Converts `Some` to `None` if the predicate returns `false`.
841
+ *
842
+ * If `Some`, tests the predicate and returns `Some` if true, `None` if false.
843
+ * If `None`, returns `None` without calling the predicate.
844
+ *
845
+ * @param pred - A predicate function to test the contained value
846
+ * @returns `Some<T>` if predicate passes, else `None`
847
+ *
848
+ * @example
849
+ * ```ts
850
+ * ExperimentalOption.Some(5).filter(x => x > 3); // Some(5)
851
+ * ExperimentalOption.Some(5).filter(x => x > 10); // None
852
+ * ExperimentalOption.None.filter(x => x > 3); // None
853
+ *
854
+ * // Age validation
855
+ * const adult = ExperimentalOption.Some(age)
856
+ * .filter(a => a >= 18);
857
+ *
858
+ * // Multiple filters
859
+ * const valid = ExperimentalOption.Some(password)
860
+ * .filter(pw => pw.length >= 8)
861
+ * .filter(pw => /[A-Z]/.test(pw))
862
+ * .filter(pw => /[0-9]/.test(pw));
863
+ * ```
864
+ *
865
+ * @see {@link filterAsync} for async predicates
866
+ * @see {@link fromPredicate} for creating ExperimentalOptions from predicates
867
+ */
868
+ filter(pred: (val: T) => boolean): ExperimentalOption<T>;
869
+ /**
870
+ * Converts `Some` to `None` if the async predicate returns `false`.
871
+ *
872
+ * @param pred - An async predicate function to test the contained value
873
+ * @returns `Promise<Some<T>>` if predicate passes, else `Promise<None>`
874
+ *
875
+ * @example
876
+ * ```ts
877
+ * // Async validation
878
+ * const valid = await ExperimentalOption.Some(email)
879
+ * .filterAsync(async e => await checkEmailDomain(e));
880
+ *
881
+ * // Practical: database validation
882
+ * const unique = await ExperimentalOption.Some(username)
883
+ * .filterAsync(async u => await isUsernameAvailable(u));
884
+ * ```
885
+ *
886
+ * @see {@link filter} for synchronous predicates
887
+ */
888
+ filterAsync(pred: (val: T) => Promise<boolean>): Promise<ExperimentalOption<T>>;
889
+ /**
890
+ * Pairs the original value with a derived value.
891
+ *
892
+ * Creates a tuple `[original, derived]` where the second value is computed
893
+ * from the first using the provided function.
894
+ *
895
+ * This method rejects async mappers at compile time - use {@link zipAsync} for those.
896
+ *
897
+ * @template U - The type of the derived value
898
+ * @param fn - A function that derives a value from the contained value
899
+ * @returns `Some<[T, U]>` with the tuple, or `None`
900
+ *
901
+ * @example
902
+ * ```ts
903
+ * ExperimentalOption.Some(5).zip(x => x * 2); // Some([5, 10])
904
+ * ExperimentalOption.None.zip(x => x * 2); // None
905
+ *
906
+ * // Keep original while computing derived value
907
+ * ExperimentalOption.Some(user).zip(u => u.permissions.length);
908
+ * // Some([user, 5])
909
+ *
910
+ * // Practical: price with discount
911
+ * const priceWithDiscount = ExperimentalOption.Some(100)
912
+ * .zip(price => price * 0.9);
913
+ * // Some([100, 90])
914
+ *
915
+ * // Derive metadata while keeping original
916
+ * ExperimentalOption.Some(request).zip(req => ({
917
+ * size: JSON.stringify(req).length,
918
+ * timestamp: Date.now()
919
+ * }));
920
+ * // Some([request, { size, timestamp }])
921
+ * ```
922
+ *
923
+ * @see {@link zipAsync} for async derivation
924
+ * @see {@link flatZip} for combining with another ExperimentalOption
925
+ */
926
+ zip<U>(fn: (val: T) => Promise<U>): never;
927
+ zip<U>(fn: (val: T) => U): ExperimentalOption<[prev: T, curr: U]>;
928
+ /**
929
+ * Pairs the original value with an async derived value.
930
+ *
931
+ * @template U - The type of the derived value
932
+ * @param fn - An async function that derives a value from the contained value
933
+ * @returns `Promise<Some<[T, U]>>` with the tuple, or `Promise<None>`
934
+ *
935
+ * @example
936
+ * ```ts
937
+ * const result = await ExperimentalOption.Some(user)
938
+ * .zipAsync(async u => await fetchCount(u));
939
+ * // Some([user, number])
940
+ *
941
+ * // Practical: user with additional data
942
+ * const enriched = await ExperimentalOption.Some(userId)
943
+ * .zipAsync(async id => await fetchUser(id))
944
+ * .then(o => o.zipAsync(async ([id, user]) => await getStats(user.id)));
945
+ * // Promise<ExperimentalOption<[userId, User, Stats]>>
946
+ * ```
947
+ *
948
+ * @see {@link zip} for synchronous derivation
949
+ */
950
+ zipAsync<U>(fn: (val: T) => Promise<U>): Promise<ExperimentalOption<[T, U]>>;
951
+ /**
952
+ * Pairs the original value with a value from another ExperimentalOption.
953
+ *
954
+ * Combines two independent ExperimentalOptions into a tuple. Both must be `Some` to succeed.
955
+ *
956
+ * @template U - The type of the value in the other ExperimentalOption
957
+ * @param fn - A function that returns an ExperimentalOption to combine with
958
+ * @returns `Some<[T, U]>` with both values, or `None`
959
+ *
960
+ * @example
961
+ * ```ts
962
+ * // Basic usage
963
+ * ExperimentalOption.Some(5).flatZip(x => ExperimentalOption.Some(10)); // Some([5, 10])
964
+ * ExperimentalOption.Some(5).flatZip(x => ExperimentalOption.None); // None
965
+ * ExperimentalOption.None.flatZip(x => ExperimentalOption.Some(10)); // None
966
+ *
967
+ * // Practical: combine independent lookups
968
+ * const productData = ExperimentalOption.Some(productId)
969
+ * .flatZip(id => fetchProductPrice(id));
970
+ * // ExperimentalOption<[productId, price]>
971
+ *
972
+ * // Multiple dependencies
973
+ * const complete = ExperimentalOption.Some(userId)
974
+ * .flatZip(id => fetchUser(id))
975
+ * .flatMap(([id, user]) => ExperimentalOption.Some([id, user.profileId]))
976
+ * .flatZip(([id, profileId]) => fetchProfile(profileId));
977
+ * // ExperimentalOption<[userId, profileId, profile]>
978
+ * ```
979
+ *
980
+ * @see {@link flatZipAsync} for async ExperimentalOptions
981
+ * @see {@link zip} for derived values
982
+ * @see {@link all} for combining multiple ExperimentalOptions at once
983
+ */
984
+ flatZip<U>(fn: (val: T) => ExperimentalOption<U>): ExperimentalOption<[T, U]>;
985
+ /**
986
+ * Pairs the original value with a value from an async ExperimentalOption.
987
+ *
988
+ * @template U - The type of the value in the other ExperimentalOption
989
+ * @param fn - An async function that returns an ExperimentalOption
990
+ * @returns `Promise<Some<[T, U]>>` with both values, or `Promise<None>`
991
+ *
992
+ * @example
993
+ * ```ts
994
+ * const result = await ExperimentalOption.Some(userId)
995
+ * .flatZipAsync(async id => await fetchUser(id));
996
+ * // ExperimentalOption<[userId, User]>
997
+ *
998
+ * // Practical: async pipeline
999
+ * const enriched = await ExperimentalOption.Some(userId)
1000
+ * .flatZipAsync(async id => await fetchUser(id))
1001
+ * .then(o => o.flatMapAsync(async ([id, user]) =>
1002
+ * ExperimentalOption.Some([id, user, await fetchProfile(user.profileId)])
1003
+ * ));
1004
+ * ```
1005
+ *
1006
+ * @see {@link flatZip} for synchronous ExperimentalOptions
1007
+ */
1008
+ flatZipAsync<U>(fn: (val: T) => Promise<ExperimentalOption<U>>): Promise<ExperimentalOption<[T, U]>>;
1009
+ /**
1010
+ * Executes a side effect function if `Some`, returning `this` unchanged.
1011
+ *
1012
+ * Useful for logging, debugging, or executing effects without breaking the chain.
1013
+ *
1014
+ * @param fn - A side effect function to execute with the contained value
1015
+ * @returns `this` (unchanged, for chaining)
1016
+ *
1017
+ * @example
1018
+ * ```ts
1019
+ * // Logging without breaking chain
1020
+ * ExperimentalOption.Some(user)
1021
+ * .tap(u => console.log(`Processing ${u.name}`))
1022
+ * .map(u => u.email);
1023
+ *
1024
+ * // Debugging
1025
+ * ExperimentalOption.Some(input)
1026
+ * .tap(val => console.log("Before:", val))
1027
+ * .map(transform)
1028
+ * .tap(val => console.log("After:", val));
1029
+ *
1030
+ * // Practical: audit trail
1031
+ * const result = ExperimentalOption.Some(transaction)
1032
+ * .tap((t) => auditLog("start", t.id))
1033
+ * .flatMap((t) => process(t))
1034
+ * .tap((r) => auditLog("success", r.id));
1035
+ * ```
1036
+ *
1037
+ * @see {@link tapAsync} for async side effects
1038
+ */
1039
+ tap<F extends (val: T) => unknown>(fn: ReturnType<F> extends PromiseLike<unknown> ? never : F): ExperimentalOption<T>;
1040
+ /**
1041
+ * Executes an async side effect function if `Some`, returning `this` unchanged.
1042
+ *
1043
+ * @param fn - An async side effect function to execute
1044
+ * @returns `Promise<ExperimentalOption<T>>` that resolves to `this`
1045
+ *
1046
+ * @example
1047
+ * ```ts
1048
+ * // Async logging
1049
+ * await ExperimentalOption.Some(user)
1050
+ * .tapAsync(async u => await logAnalytics(u.id))
1051
+ * .map(u => u.email);
1052
+ *
1053
+ * // Practical: metrics collection
1054
+ * const result = await ExperimentalOption.Some(request)
1055
+ * .tapAsync(async req => await trackMetric("request_start", req.id))
1056
+ * .flatMapAsync(async req => await processRequest(req))
1057
+ * .tapAsync(async res => await trackMetric("request_success", res.id));
1058
+ * ```
1059
+ *
1060
+ * @see {@link tap} for synchronous side effects
1061
+ */
1062
+ tapAsync(fn: (val: T) => Promise<void>): Promise<ExperimentalOption<T>>;
1063
+ /**
1064
+ * Converts this ExperimentalOption to a Result.
1065
+ *
1066
+ * `Some` becomes `Ok`, `None` becomes `Err` with the provided error.
1067
+ *
1068
+ * @template E - The type of the error for the `Err` variant
1069
+ * @param error - The error value to use if this is `None`
1070
+ * @returns `Result<T, E>` with the value or error
1071
+ *
1072
+ * @example
1073
+ * ```ts
1074
+ * ExperimentalOption.Some(42).toResult("was none"); // Ok(42)
1075
+ * ExperimentalOption.None.toResult("was none"); // Err("was none")
1076
+ *
1077
+ * // Practical: convert to Result for error handling
1078
+ * const user = ExperimentalOption.fromNullable(getUser())
1079
+ * .toResult(new Error("User not found"));
1080
+ *
1081
+ * // With custom error types
1082
+ * const config = ExperimentalOption.fromNullable(loadConfig())
1083
+ * .toResult({ code: "CONFIG_MISSING", message: "Config file not found" });
1084
+ * ```
1085
+ *
1086
+ * @see {@link ExperimentalResult} for the Result type
1087
+ */
1088
+ toResult<E>(error: E): ExperimentalResult<T, E>;
1089
+ /**
1090
+ * Maps over array elements inside an `ExperimentalOption<Array<T>>`.
1091
+ *
1092
+ * Only callable when `T` is an array type. Applies the mapper to each
1093
+ * element if `Some`, or returns `None`.
1094
+ *
1095
+ * @template Inner - The element type of the array
1096
+ * @template Out - The output element type
1097
+ * @param mapper - A function to transform each array element
1098
+ * @returns `Some<Out[]>` with mapped array, or `None`
1099
+ * @throws {TypeError} if called on a non-array ExperimentalOption
1100
+ *
1101
+ * @example
1102
+ * ```ts
1103
+ * ExperimentalOption.Some([1, 2, 3]).innerMap(n => n * 2);
1104
+ * // Some([2, 4, 6])
1105
+ *
1106
+ * ExperimentalOption.None.innerMap(n => n * 2);
1107
+ * // None
1108
+ *
1109
+ * // Practical: transform list results
1110
+ * const users = await fetchUsers() // ExperimentalOption<User[]>
1111
+ * .innerMap(u => u.name)
1112
+ * .innerMap(names => names.sort());
1113
+ * // Some<string[]>
1114
+ *
1115
+ * // Error case
1116
+ * ExperimentalOption.Some(42).innerMap(x => x * 2);
1117
+ * // throws TypeError: innerMap can only be called on ExperimentalOption<Array<T>>
1118
+ * ```
1119
+ *
1120
+ * @see {@link map} for transforming the contained value directly
1121
+ */
1122
+ innerMap<Inner, Out>(this: ExperimentalOption<Array<Inner>>, mapper: (val: Inner) => Out): ExperimentalOption<Out[]>;
1123
+ /**
1124
+ * Returns a string representation of the ExperimentalOption.
1125
+ *
1126
+ * @returns A string describing the ExperimentalOption state and value
1127
+ *
1128
+ * @example
1129
+ * ```ts
1130
+ * ExperimentalOption.Some(42).toString(); // "ExperimentalOption::Some(42)"
1131
+ * ExperimentalOption.None.toString(); // "ExperimentalOption::None"
1132
+ * ExperimentalOption.Some("hello").toString(); // "ExperimentalOption::Some(hello)"
1133
+ * ```
1134
+ */
1135
+ toString(): string;
1136
+ /**
1137
+ * Makes ExperimentalOption iterable for use with generator-based syntax.
1138
+ *
1139
+ * Yields self and returns the unwrapped value when resumed.
1140
+ * Used internally by {@link ExperimentalOption.gen} for `yield*` syntax.
1141
+ *
1142
+ * @example
1143
+ * ```ts
1144
+ * const result = ExperimentalOption.gen(function* () {
1145
+ * const value = yield* ExperimentalOption.Some(42);
1146
+ * return value * 2;
1147
+ * });
1148
+ * // Some(84)
1149
+ * ```
1150
+ *
1151
+ * @internal
1152
+ */
1153
+ [Symbol.iterator](): Generator<ExperimentalOption<T>, T, unknown>;
1154
+ /**
1155
+ * Makes ExperimentalOption iterable for use with async generator-based syntax.
1156
+ *
1157
+ * Yields self and returns the unwrapped value when resumed.
1158
+ * Used internally by {@link ExperimentalOption.asyncGen} for `yield*` syntax.
1159
+ *
1160
+ * @example
1161
+ * ```ts
1162
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1163
+ * const value = yield* await Promise.resolve(ExperimentalOption.Some(42));
1164
+ * return value * 2;
1165
+ * });
1166
+ * // Some(84)
1167
+ * ```
1168
+ *
1169
+ * @internal
1170
+ */
1171
+ [Symbol.asyncIterator](): AsyncGenerator<ExperimentalOption<T>, Awaited<T>, unknown>;
1172
+ /**
1173
+ * Generator-based syntax for chaining ExperimentalOption operations (simplified, no adapter).
1174
+ *
1175
+ * Provides imperative-style code while maintaining functional ExperimentalOption handling.
1176
+ * Use `yield*` to unwrap ExperimentalOptions or short-circuit on `None`.
1177
+ *
1178
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
1179
+ * instead of recursion to avoid stack overflow on deep chains.
1180
+ *
1181
+ * @template T - The return type of the generator
1182
+ * @param genFn - A generator function that yields ExperimentalOptions
1183
+ * @returns `Some<T>` with the generator's return value, or `None`
1184
+ *
1185
+ * @example
1186
+ * ```ts
1187
+ * // Simple sync chain
1188
+ * const result = ExperimentalOption.gen(function* () {
1189
+ * const a = yield* ExperimentalOption.Some(1);
1190
+ * const b = yield* ExperimentalOption.Some(2);
1191
+ * return a + b;
1192
+ * });
1193
+ * // Some(3)
1194
+ *
1195
+ * // None short-circuit
1196
+ * const shortCircuit = ExperimentalOption.gen(function* () {
1197
+ * const a = yield* ExperimentalOption.Some(1);
1198
+ * const b = yield* ExperimentalOption.None; // Short-circuits here
1199
+ * const c = yield* ExperimentalOption.Some(3); // Never executes
1200
+ * return a + b + c;
1201
+ * });
1202
+ * // None
1203
+ *
1204
+ * // Chaining optional operations
1205
+ * const email = ExperimentalOption.gen(function* () {
1206
+ * const user = yield* findUser(userId);
1207
+ * const profile = yield* ExperimentalOption.fromNullable(user.profile);
1208
+ * const settings = yield* ExperimentalOption.fromNullable(profile.settings);
1209
+ * return settings.email;
1210
+ * });
1211
+ *
1212
+ * // Nested optional access
1213
+ * const city = ExperimentalOption.gen(function* () {
1214
+ * const user = yield* ExperimentalOption.fromNullable(getUser());
1215
+ * const address = yield* ExperimentalOption.fromNullable(user?.address);
1216
+ * const city = yield* ExperimentalOption.fromNullable(address?.city);
1217
+ * return city;
1218
+ * });
1219
+ * ```
1220
+ *
1221
+ * @see {@link genAdapter} for better type inference in complex chains
1222
+ * @see {@link asyncGen} for async operations
1223
+ */
1224
+ static gen<T>(this: void, genFn: () => Generator<ExperimentalOption<unknown>, T, unknown>): ExperimentalOption<T>;
1225
+ /**
1226
+ * Generator-based syntax for chaining ExperimentalOption operations with an adapter function.
1227
+ *
1228
+ * Provides better type inference and IDE support compared to {@link gen}.
1229
+ * The `$` adapter wraps ExperimentalOptions and enables clearer type tracking.
1230
+ *
1231
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
1232
+ * instead of recursion to avoid stack overflow on deep chains.
1233
+ *
1234
+ * @template Eff - The effect type (inferred from usage)
1235
+ * @template T - The return type of the generator
1236
+ * @param genFn - A generator function receiving an adapter `$`
1237
+ * @returns `Some<T>` with the generator's return value, or `None`
1238
+ *
1239
+ * @example
1240
+ * ```ts
1241
+ * // Better type inference
1242
+ * const result = ExperimentalOption.genAdapter(function* ($) {
1243
+ * const a = yield* $(ExperimentalOption.Some(1));
1244
+ * const b = yield* $(ExperimentalOption.Some(2));
1245
+ * return a + b;
1246
+ * });
1247
+ * // Some(3)
1248
+ *
1249
+ * // Deep nested access with better type safety
1250
+ * const email = ExperimentalOption.genAdapter(function* ($) {
1251
+ * const user = yield* $(ExperimentalOption.fromNullable(apiResponse?.user));
1252
+ * const profile = yield* $(ExperimentalOption.fromNullable(user?.profile));
1253
+ * const contact = yield* $(ExperimentalOption.fromNullable(profile?.contact));
1254
+ * return contact?.email;
1255
+ * });
1256
+ *
1257
+ * // Complex validation chain
1258
+ * const validConfig = ExperimentalOption.genAdapter(function* ($) {
1259
+ * const raw = yield* $(loadConfig());
1260
+ * const parsed = yield* $(parseConfig(raw));
1261
+ * const validated = yield* $(validateConfig(parsed));
1262
+ * return validated;
1263
+ * });
1264
+ * ```
1265
+ *
1266
+ * @see {@link gen} for simpler chains without adapter
1267
+ * @see {@link asyncGenAdapter} for async operations
1268
+ */
1269
+ static genAdapter<Eff extends OptionYieldWrap<any>, T>(this: void, genFn: (adapter: <A>(option: ExperimentalOption<A>) => OptionYieldWrap<A>) => Generator<Eff, T, any>): ExperimentalOption<T>;
1270
+ /**
1271
+ * Async generator-based syntax for chaining ExperimentalOption operations (simplified, no adapter).
1272
+ *
1273
+ * Use `yield*` with `ExperimentalOption<T>` values directly. For `Promise<ExperimentalOption<T>>`,
1274
+ * await first then `yield*`.
1275
+ *
1276
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
1277
+ * instead of recursion to avoid stack overflow on deep chains.
1278
+ *
1279
+ * @template T - The return type of the generator
1280
+ * @param genFn - An async generator function that yields ExperimentalOptions
1281
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
1282
+ *
1283
+ * @example
1284
+ * ```ts
1285
+ * // Simple async chain
1286
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1287
+ * const a = yield* ExperimentalOption.Some(1);
1288
+ * const b = yield* await asyncOperation(a); // await Promise<ExperimentalOption> first
1289
+ * const c = yield* ExperimentalOption.Some(3);
1290
+ * return a + b + c;
1291
+ * });
1292
+ * // Some(result)
1293
+ *
1294
+ * // None short-circuit in async
1295
+ * const shortCircuit = await ExperimentalOption.asyncGen(async function* () {
1296
+ * const data = yield* await fetchOptionalData();
1297
+ * const parsed = yield* parse(data); // Short-circuits on None
1298
+ * const validated = yield* validate(parsed); // Never executes
1299
+ * return validated;
1300
+ * });
1301
+ * // None
1302
+ *
1303
+ * // Mixed sync/async workflow
1304
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1305
+ * const id = yield* ExperimentalOption.Some(parseInt(input)); // sync
1306
+ * const user = yield* await fetchUser(id); // async
1307
+ * const profile = yield* ExperimentalOption.fromNullable(user?.profile); // sync
1308
+ * const enriched = yield* await enrichProfile(profile); // async
1309
+ * return enriched;
1310
+ * });
1311
+ *
1312
+ * // Complex pipeline with multiple optional steps
1313
+ * async function processLead(email: string): Promise<ExperimentalOption<Lead>> {
1314
+ * return await ExperimentalOption.asyncGen(async function* () {
1315
+ * const normalized = yield* ExperimentalOption.Some(normalizeEmail(email));
1316
+ * const existing = yield* await findExistingLead(normalized);
1317
+ * const enriched = yield* await enrichLeadData(existing);
1318
+ * const validated = yield* validateLead(enriched);
1319
+ * return validated;
1320
+ * });
1321
+ * }
1322
+ * ```
1323
+ *
1324
+ * @see {@link asyncGenAdapter} for better type inference and cleaner syntax
1325
+ * @see {@link gen} for synchronous operations
1326
+ */
1327
+ static asyncGen<T>(this: void, genFn: () => AsyncGenerator<ExperimentalOption<unknown>, T, unknown>): Promise<ExperimentalOption<T>>;
1328
+ /**
1329
+ * Async generator-based syntax for chaining ExperimentalOption operations with an adapter function.
1330
+ *
1331
+ * The `$` adapter handles both sync `ExperimentalOption<T>` and async `Promise<ExperimentalOption<T>>`,
1332
+ * automatically awaiting promises. This provides cleaner syntax for mixed sync/async workflows.
1333
+ *
1334
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
1335
+ * instead of recursion to avoid stack overflow on deep chains.
1336
+ *
1337
+ * @template Eff - The effect type (inferred from usage)
1338
+ * @template T - The return type of the generator
1339
+ * @param genFn - An async generator function receiving an adapter `$`
1340
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
1341
+ *
1342
+ * @example
1343
+ * ```ts
1344
+ * // No need to manually await - adapter handles it
1345
+ * const result = await ExperimentalOption.asyncGenAdapter(async function* ($) {
1346
+ * const a = yield* $(ExperimentalOption.Some(1)); // sync ExperimentalOption
1347
+ * const b = yield* $(asyncOperation(a)); // Promise<ExperimentalOption> - auto-awaited
1348
+ * const c = yield* $(ExperimentalOption.Some(3));
1349
+ * return a + b + c;
1350
+ * });
1351
+ *
1352
+ * // Complex workflow with database and API calls
1353
+ * const userData = await ExperimentalOption.asyncGenAdapter(async function* ($) {
1354
+ * const session = yield* $(getSession()); // Promise<ExperimentalOption<Session>>
1355
+ * const userId = yield* $(ExperimentalOption.fromNullable(session?.userId));
1356
+ * const profile = yield* $(await fetchProfile(userId)); // Promise<ExperimentalOption<Profile>>
1357
+ * const preferences = yield* $(ExperimentalOption.fromNullable(profile?.preferences));
1358
+ * const enriched = yield* $(await enrichPreferences(preferences));
1359
+ * return enriched;
1360
+ * });
1361
+ *
1362
+ * // Real-world: Optional data enrichment
1363
+ * async function getProductDetails(productId: string): Promise<ExperimentalOption<ProductDetails>> {
1364
+ * return await ExperimentalOption.asyncGenAdapter(async function* ($) {
1365
+ * const product = yield* $(await fetchProduct(productId));
1366
+ * const pricing = yield* $(await fetchPricing(product.sku));
1367
+ * const inventory = yield* $(await checkInventory(product.id));
1368
+ * const reviews = yield* $(await fetchReviews(product.id));
1369
+ * const related = yield* $(await fetchRelatedProducts(product.category));
1370
+ *
1371
+ * return { product, pricing, inventory, reviews, related };
1372
+ * });
1373
+ * }
1374
+ * ```
1375
+ *
1376
+ * @see {@link asyncGen} when you want explicit `await` for async operations
1377
+ * @see {@link genAdapter} for synchronous operations
1378
+ */
1379
+ static asyncGenAdapter<Eff extends AsyncOptionYieldWrap<any>, T>(this: void, genFn: (adapter: <A>(option: ExperimentalOption<A> | Promise<ExperimentalOption<A>>) => AsyncOptionYieldWrap<A>) => AsyncGenerator<Eff, T, any>): Promise<ExperimentalOption<T>>;
1380
+ }
1381
+ //#endregion
1382
+ export { ExperimentalOption, UnitOption, UnwrapOption, UnwrappedNone };
1383
+ //# sourceMappingURL=option-experimental.d.cts.map