@onrails/result 0.4.2 → 0.5.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.
package/dist/railway.js CHANGED
@@ -1,12 +1,86 @@
1
- // src/internal/ctor.ts
2
- var mkOk = (value) => ({
3
- _tag: "Ok",
4
- value
5
- });
6
- var mkErr = (error) => ({
7
- _tag: "Err",
8
- error
9
- });
1
+ // src/internal/proto.ts
2
+ var INSPECT = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
3
+ var OkImpl = class _OkImpl {
4
+ _tag;
5
+ value;
6
+ // explicit assignment order keeps `_tag` first, so JSON key order matches the plain-data shape
7
+ constructor(value) {
8
+ this._tag = "Ok";
9
+ this.value = value;
10
+ }
11
+ [INSPECT](_depth, options, inspect) {
12
+ return `Ok(${inspect(this.value, options)})`;
13
+ }
14
+ map(fn) {
15
+ return new _OkImpl(fn(this.value));
16
+ }
17
+ mapErr(_fn) {
18
+ return new _OkImpl(this.value);
19
+ }
20
+ flatMap(fn) {
21
+ return fn(this.value);
22
+ }
23
+ andThen(fn) {
24
+ return fn(this.value);
25
+ }
26
+ recover(_fn) {
27
+ return new _OkImpl(this.value);
28
+ }
29
+ tap(fn) {
30
+ fn(this.value);
31
+ return this;
32
+ }
33
+ tapErr(_fn) {
34
+ return this;
35
+ }
36
+ match(onOk, _onErr) {
37
+ return onOk(this.value);
38
+ }
39
+ unwrapOr(_defaultValue) {
40
+ return this.value;
41
+ }
42
+ };
43
+ var ErrImpl = class _ErrImpl {
44
+ _tag;
45
+ error;
46
+ constructor(error) {
47
+ this._tag = "Err";
48
+ this.error = error;
49
+ }
50
+ [INSPECT](_depth, options, inspect) {
51
+ return `Err(${inspect(this.error, options)})`;
52
+ }
53
+ map(_fn) {
54
+ return new _ErrImpl(this.error);
55
+ }
56
+ mapErr(fn) {
57
+ return new _ErrImpl(fn(this.error));
58
+ }
59
+ flatMap(_fn) {
60
+ return new _ErrImpl(this.error);
61
+ }
62
+ andThen(_fn) {
63
+ return new _ErrImpl(this.error);
64
+ }
65
+ recover(fn) {
66
+ return fn(this.error);
67
+ }
68
+ tap(_fn) {
69
+ return this;
70
+ }
71
+ tapErr(fn) {
72
+ fn(this.error);
73
+ return this;
74
+ }
75
+ match(_onOk, onErr) {
76
+ return onErr(this.error);
77
+ }
78
+ unwrapOr(defaultValue) {
79
+ return defaultValue;
80
+ }
81
+ };
82
+ var newOk = (value) => new OkImpl(value);
83
+ var newErr = (error) => new ErrImpl(error);
10
84
 
11
85
  // src/internal/dual.ts
12
86
  var dual = (arity, body) => {
@@ -20,26 +94,26 @@ var isOk = (result) => result._tag === "Ok";
20
94
  var isErr = (result) => result._tag === "Err";
21
95
  var map = dual(
22
96
  2,
23
- (result, fn) => isOk(result) ? mkOk(fn(result.value)) : mkErr(result.error)
97
+ (result, fn) => isOk(result) ? newOk(fn(result.value)) : newErr(result.error)
24
98
  );
25
99
  var mapErr = dual(
26
100
  2,
27
- (result, fn) => isErr(result) ? mkErr(fn(result.error)) : mkOk(result.value)
101
+ (result, fn) => isErr(result) ? newErr(fn(result.error)) : newOk(result.value)
28
102
  );
29
103
  var bimap = dual(
30
104
  3,
31
- (result, onOk, onErr) => isOk(result) ? mkOk(onOk(result.value)) : mkErr(onErr(result.error))
105
+ (result, onOk, onErr) => isOk(result) ? newOk(onOk(result.value)) : newErr(onErr(result.error))
32
106
  );
33
107
  var flatMap = dual(
34
108
  2,
35
- (result, fn) => isOk(result) ? fn(result.value) : mkErr(result.error)
109
+ (result, fn) => isOk(result) ? fn(result.value) : newErr(result.error)
36
110
  );
37
111
  function fromThrowable(fn, onThrow) {
38
112
  return (...args) => {
39
113
  try {
40
- return mkOk(fn(...args));
114
+ return newOk(fn(...args));
41
115
  } catch (error) {
42
- return mkErr(onThrow(error));
116
+ return newErr(onThrow(error));
43
117
  }
44
118
  };
45
119
  }
@@ -59,11 +133,11 @@ var sequenceSettled = (settled) => {
59
133
  const values = [];
60
134
  for (const result of settled) {
61
135
  if (isErr(result)) {
62
- return mkErr(result.error);
136
+ return newErr(result.error);
63
137
  }
64
138
  values.push(result.value);
65
139
  }
66
- return mkOk(values);
140
+ return newOk(values);
67
141
  };
68
142
  var ResultAsync = class _ResultAsync {
69
143
  constructor(run) {
@@ -100,9 +174,9 @@ var ResultAsync = class _ResultAsync {
100
174
  static fromPromise(promise, onReject) {
101
175
  return new _ResultAsync(async () => {
102
176
  try {
103
- return mkOk(await promise);
177
+ return newOk(await promise);
104
178
  } catch (error) {
105
- return mkErr(onReject(error));
179
+ return newErr(onReject(error));
106
180
  }
107
181
  });
108
182
  }
@@ -116,7 +190,7 @@ var ResultAsync = class _ResultAsync {
116
190
  * ```
117
191
  */
118
192
  static fromSafePromise(promise) {
119
- return new _ResultAsync(async () => mkOk(await promise));
193
+ return new _ResultAsync(async () => newOk(await promise));
120
194
  }
121
195
  /**
122
196
  * Defers work until {@link resolve}. Unlike {@link fromPromise}, the factory
@@ -134,13 +208,13 @@ var ResultAsync = class _ResultAsync {
134
208
  return new _ResultAsync(fn);
135
209
  }
136
210
  static ok(value) {
137
- return new _ResultAsync(async () => mkOk(value));
211
+ return new _ResultAsync(async () => newOk(value));
138
212
  }
139
213
  static of(value) {
140
214
  return _ResultAsync.ok(value);
141
215
  }
142
216
  static err(error) {
143
- return new _ResultAsync(async () => mkErr(error));
217
+ return new _ResultAsync(async () => newErr(error));
144
218
  }
145
219
  /**
146
220
  * Lifts an existing `Promise<Result<T, E>>` (e.g. from interop code) into a
@@ -158,7 +232,7 @@ var ResultAsync = class _ResultAsync {
158
232
  try {
159
233
  return await promise;
160
234
  } catch (error) {
161
- return mkErr(mapDefect(error));
235
+ return newErr(mapDefect(error));
162
236
  }
163
237
  });
164
238
  }
@@ -180,11 +254,11 @@ var ResultAsync = class _ResultAsync {
180
254
  for (const ra of results) {
181
255
  const result = await ra.resolve();
182
256
  if (isErr(result)) {
183
- return mkErr(result.error);
257
+ return newErr(result.error);
184
258
  }
185
259
  values.push(result.value);
186
260
  }
187
- return mkOk(values);
261
+ return newOk(values);
188
262
  });
189
263
  }
190
264
  /**
@@ -279,7 +353,7 @@ var ResultAsync = class _ResultAsync {
279
353
  return new _ResultAsync(async () => {
280
354
  const first = await this.resolve();
281
355
  if (isErr(first)) {
282
- return first;
356
+ return newErr(first.error);
283
357
  }
284
358
  const next = fn(first.value);
285
359
  return next instanceof _ResultAsync ? next.resolve() : next;
@@ -311,7 +385,7 @@ var ResultAsync = class _ResultAsync {
311
385
  return new _ResultAsync(async () => {
312
386
  const first = await this.resolve();
313
387
  if (!isErr(first)) {
314
- return first;
388
+ return newOk(first.value);
315
389
  }
316
390
  const next = fn(first.error);
317
391
  return next instanceof _ResultAsync ? next.resolve() : next;
@@ -437,11 +511,11 @@ var Railway = class _Railway {
437
511
  state;
438
512
  /** Start an empty sync workflow with no fields in context. */
439
513
  static empty() {
440
- return new _Railway({ mode: "sync", result: mkOk({}) });
514
+ return new _Railway({ mode: "sync", result: newOk({}) });
441
515
  }
442
516
  /** Start a sync workflow with the given context as the initial state. */
443
517
  static context(context) {
444
- return new _Railway({ mode: "sync", result: mkOk(context) });
518
+ return new _Railway({ mode: "sync", result: newOk(context) });
445
519
  }
446
520
  /** Start a sync workflow with a throwing function. */
447
521
  static fromSync(key, fn, onThrow) {
@@ -504,7 +578,7 @@ var Railway = class _Railway {
504
578
  }
505
579
  /** Pure sync derivation. */
506
580
  derive(key, fn, ..._guard) {
507
- return this.addSync(key, (ctx) => mkOk(fn(ctx)));
581
+ return this.addSync(key, (ctx) => newOk(fn(ctx)));
508
582
  }
509
583
  /** Throwing sync transform. */
510
584
  fromSync(key, fn, onThrow, ..._guard) {
@@ -526,7 +600,7 @@ var Railway = class _Railway {
526
600
  require(key, source, onMissing, ..._guard) {
527
601
  return this.addSync(key, (ctx) => {
528
602
  const value = ctx[source];
529
- return value == null ? mkErr(onMissing(ctx)) : mkOk(value);
603
+ return value == null ? newErr(onMissing(ctx)) : newOk(value);
530
604
  });
531
605
  }
532
606
  /** Run independent `ResultAsync` branches concurrently and merge outputs. */
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/internal/ctor.ts","../src/internal/dual.ts","../src/result.ts","../src/types.ts","../src/async.ts","../src/railway.ts"],"names":[],"mappings":";AASO,IAAM,IAAA,GAAO,CAAe,KAAA,MAA4B;AAAA,EAC7D,IAAA,EAAM,IAAA;AAAA,EACN;AACF,CAAA,CAAA;AAGO,IAAM,KAAA,GAAQ,CAAyB,KAAA,MAA4B;AAAA,EACxE,IAAA,EAAM,KAAA;AAAA,EACN;AACF,CAAA,CAAA;;;ACwBO,IAAM,IAAA,GAAO,CAAI,KAAA,EAAe,IAAA,KAA2C;AAMhF,EAAA,MAAM,GAAA,GAAM,IAAA;AACZ,EAAA,MAAM,UAAA,GAAa,CAAA,GAAI,IAAA,KACrB,IAAA,CAAK,UAAU,KAAA,GAAQ,GAAA,CAAI,GAAG,IAAI,IAAI,CAAC,IAAA,KAAkB,GAAA,CAAI,IAAA,EAAM,GAAG,IAAI,CAAA;AAG5E,EAAA,OAAO,UAAA;AACT,CAAA;;;ACyBO,IAAM,IAAA,GAAO,CAAO,MAAA,KAA6C,MAAA,CAAO,IAAA,KAAS,IAAA;AAYjF,IAAM,KAAA,GAAQ,CAAO,MAAA,KAA8C,MAAA,CAAO,IAAA,KAAS,KAAA;AAoBnF,IAAM,GAAA,GAGT,IAAA;AAAA,EACF,CAAA;AAAA,EACA,CAAU,MAAA,EAAsB,EAAA,KAC9B,IAAA,CAAK,MAAM,CAAA,GAAI,IAAA,CAAK,EAAA,CAAG,MAAA,CAAO,KAAK,CAAC,CAAA,GAAI,KAAA,CAAM,OAAO,KAAK;AAC9D,CAAA;AAeO,IAAM,MAAA,GAGT,IAAA;AAAA,EACF,CAAA;AAAA,EACA,CAAU,MAAA,EAAsB,EAAA,KAC9B,KAAA,CAAM,MAAM,CAAA,GAAI,KAAA,CAAM,EAAA,CAAG,MAAA,CAAO,KAAK,CAAC,CAAA,GAAI,IAAA,CAA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ACxF,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,OAAA,EAAQ,CAAE,OAAA;AAAA,MAAQ,CAAC,QACrC,WAAA,CAAY,oBAAA;AAAA,QACV,MAAA,CAAO,OAAA,CAAQ,QAAQ,CAAA,CAAE,GAAA,CAAI,CAAC,GAAG,MAAM,CAAA,KAAM,MAAA,CAAO,GAAG,CAAC;AAAA,OAC1D,CAAE,GAAA;AAAA,QACA,CAAC,MAAA;AAAA;AAAA;AAAA,UAGE;AAAA,YACC,GAAG,GAAA;AAAA,YACH,GAAG,MAAA,CAAO,WAAA,CAAY,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA,CAAE,GAAA,CAAI,CAAC,GAAA,EAAK,UAAU,CAAC,GAAA,EAAK,OAAO,KAAK,CAAC,CAAC,CAAC;AAAA;AACvF;AAAA;AACJ,KACF;AACA,IAAA,OAAO,IAAI,QAAA,CAAQ;AAAA,MACjB,IAAA,EAAM,OAAA;AAAA;AAAA;AAAA;AAAA,MAIN,MAAA,EAAQ;AAAA,KAIT,CAAA;AAAA,EACH;AAAA;AAAA,EAGA,OAAU,EAAA,EAA2C;AACnD,IAAA,OAAO,IAAA,CAAK,GAAA;AAAA,MACV,CAAC,MAAA,KAAW,GAAA,CAAI,MAAA,EAAQ,EAAE,CAAA;AAAA,MAC1B,CAAC,MAAA,KAAW,MAAA,CAAO,GAAA,CAAI,EAAE;AAAA,KAC3B;AAAA,EACF;AAAA;AAAA,EAGA,IAAA,GAA+B;AAC7B,IAAA,OAAO,IAAA,CAAK,GAAA;AAAA,MACV,CAAC,MAAA,KAAW,MAAA;AAAA,MACZ,CAAC,MAAA,KAAW;AAAA,KACd;AAAA,EACF;AACF","file":"railway.js","sourcesContent":["import type { Result } from \"../types.js\";\n\n/**\n * Unlocked `Ok` constructor for library internals.\n *\n * The public {@link ok} takes a `const` type parameter so caller literals stay\n * narrow. Internals must never narrow a payload type, so they build results\n * through this constructor instead.\n */\nexport const mkOk = <T, E = never>(value: T): Result<T, E> => ({\n _tag: \"Ok\",\n value,\n});\n\n/** Unlocked `Err` constructor for library internals. Mirror of {@link mkOk}. */\nexport const mkErr = <T = never, E = unknown>(error: E): Result<T, E> => ({\n _tag: \"Err\",\n error,\n});\n","/**\n * @onrails/result internal — owning module for the **dual-form idiom**.\n *\n * Every dual-form transform in this package (`map`, `flatMap`, `match`, …)\n * accepts either shape:\n *\n * data-first: `map(result, fn)` — full arity, runs immediately\n * curried: `map(fn)(result)` — arity minus the leading `self`, returns\n * a unary function awaiting the data\n *\n * {@link dual} is the single arity dispatcher those transforms derive from;\n * hand-rolled `args.length` checks are not allowed elsewhere.\n *\n * NOTE: a twin copy of this module lives at `packages/maybe/src/internal/dual.ts`\n * — keep the two in sync when editing.\n *\n * Internal only: not listed in package.json `exports` or tsup entries.\n */\n\n/**\n * Derive a dual-form (data-first / curried) function from a single\n * data-first `body`.\n *\n * When called with `args.length >= arity` the wrapper runs `body(...args)`\n * (data-first). With fewer arguments it returns `(self) => body(self, ...args)`\n * — the curried, data-last-`self` form used with `pipe`/`flow`.\n *\n * The public type is supplied entirely by the caller: annotate the exported\n * const with the exact overload set and let `F` be inferred from it.\n *\n * @example\n * ```ts\n * export const map: {\n * <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;\n * <T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;\n * } = dual(\n * 2,\n * <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> =>\n * isOk(result) ? ok(fn(result.value)) : err(result.error),\n * );\n * ```\n */\nexport const dual = <F>(arity: number, body: (...args: never[]) => unknown): F => {\n // `(...args: never[]) => unknown` is the top function type, so every typed\n // body is assignable to the parameter — but it cannot be invoked with\n // `unknown` arguments. Re-view it through the variadic shape the dispatcher\n // actually forwards; the constraint is that `body` only ever receives the\n // arguments the caller's overload annotation admitted.\n const run = body as (...args: readonly unknown[]) => unknown;\n const dispatcher = (...args: readonly unknown[]): unknown =>\n args.length >= arity ? run(...args) : (self: unknown) => run(self, ...args);\n // The caller's overload annotation on the exported const supplies the real\n // public type; internally the dispatcher stays `unknown`-typed.\n return dispatcher as F;\n};\n","import { mkErr, mkOk } from \"./internal/ctor.js\";\nimport { dual } from \"./internal/dual.js\";\nimport type { Payload } from \"./internal/payload.js\";\nimport { toError } from \"./internal/to-error.js\";\nimport type { Err, Ok, Result } from \"./types.js\";\n\nexport type { Err, Ok, Result } from \"./types.js\";\n\n/**\n * Lifts a value into the success track.\n *\n * @example\n * ```ts\n * const r = ok(42); // Result<42, never> — literal locked\n * const typed: Result<number, \"parse\"> = ok(1);\n * const lift = <T, E>(v: T): Result<T, E> => ok(v); // generic T passes through\n * ```\n *\n * @remarks\n * An inline literal keeps its narrow type instead of widening (`ok(\"a\")` is\n * `Result<\"a\", never>`, not `Result<string, never>`), and an array literal\n * stays a mutable tuple (`ok([1, 2])` satisfies `Result<number[], E>`). A\n * generic `T` always comes back as `T`. Passing both type arguments opts out\n * of locking entirely.\n */\nexport function ok<const T extends Payload[]>(value: T): Result<T, never>;\nexport function ok<const T>(value: T): Result<T, never>;\nexport function ok<T, E>(value: T): Result<T, E>;\nexport function ok<T>(value: T): Result<T, never> {\n return mkOk(value);\n}\n\n/**\n * Fantasy Land `pure` — alias of {@link ok}. One lift name shared across the\n * trio (`of` / `Maybe.of` / `ResultAsync.of`) for generic and FL-style code.\n *\n * @example\n * ```ts\n * const r = of(42); // Result<42, never> — identical to ok(42)\n * ```\n */\nexport const of = ok;\n\n/**\n * Lifts a value into the error track.\n *\n * @example\n * ```ts\n * const r = err({ kind: \"parse\", message: \"bad json\" });\n * // Result<never, { readonly kind: \"parse\"; readonly message: string }>\n *\n * type AppError = { kind: \"parse\" } | { kind: \"io\" };\n * err<AppError>({ kind: \"io\" }); // Result<never, AppError> — one arg names the error\n * err<number, AppError>({ kind: \"io\" }); // Result<number, AppError> — neverthrow's T, E order\n * ```\n *\n * @remarks\n * One explicit type argument names the **error** channel, which is what the\n * call site almost always means. Both channels still take neverthrow's `T, E`\n * order. Inference locks literals the same way as {@link ok}, and a generic\n * `E` always comes back as `E`.\n */\nexport function err<const E extends Payload[]>(error: E): Result<never, E>;\nexport function err<const E>(error: E): Result<never, E>;\nexport function err<T, E>(error: E): Result<T, E>;\nexport function err<E>(error: E): Result<never, E> {\n return mkErr(error);\n}\n\n/**\n * Type-narrowing predicate: returns `true` when the result is `Ok`.\n *\n * @example\n * ```ts\n * if (isOk(r)) {\n * console.log(r.value); // narrowed to Ok branch\n * }\n * ```\n */\nexport const isOk = <T, E>(result: Result<T, E>): result is Ok<T, E> => result._tag === \"Ok\";\n\n/**\n * Type-narrowing predicate: returns `true` when the result is `Err`.\n *\n * @example\n * ```ts\n * if (isErr(r)) {\n * metrics.inc(\"error\", { kind: r.error.kind });\n * }\n * ```\n */\nexport const isErr = <T, E>(result: Result<T, E>): result is Err<T, E> => result._tag === \"Err\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each export accepts either shape:\n// data-first: `map(result, fn)`\n// curried: `map(fn)(result)`\n// Arity dispatch is derived from the internal `dual` combinator\n// (src/internal/dual.ts); the const annotation carries the public overloads.\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Transform the `Ok` value, passing `Err` through unchanged. Dual-form:\n * call data-first or curried (for use with {@link pipe}).\n *\n * @example\n * ```ts\n * map(ok(2), (n) => n * 3); // Ok 6 — data-first\n * pipe(ok(\"x\"), map((s) => s.length));// Ok 1 — curried\n * ```\n */\nexport const map: {\n <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;\n <T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;\n} = dual(\n 2,\n <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> =>\n isOk(result) ? mkOk(fn(result.value)) : mkErr(result.error),\n);\n\n/**\n * Transform the `Err` value, passing `Ok` through unchanged. Useful for\n * unifying heterogeneous failure types into one app-level union.\n *\n * @example\n * ```ts\n * type AppError = { kind: \"http\"; status: number } | { kind: \"parse\" };\n * pipe(\n * fetchSync(url), // Result<Body, { status: number }>\n * mapErr((e): AppError => ({ kind: \"http\", status: e.status })),\n * );\n * ```\n */\nexport const mapErr: {\n <T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;\n <E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;\n} = dual(\n 2,\n <T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> =>\n isErr(result) ? mkErr(fn(result.error)) : mkOk(result.value),\n);\n\n/**\n * Transform both tracks at once — `Ok` via `onOk`, `Err` via `onErr`.\n * Equivalent to `mapErr(onErr)(map(onOk)(result))` but in one pass.\n *\n * @example\n * ```ts\n * bimap(parsed, (cfg) => cfg.name, (e) => ({ kind: \"input\", cause: e }));\n * ```\n */\nexport const bimap: {\n <T, U, E, F>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => F): Result<U, F>;\n <T, U, E, F>(\n onOk: (value: T) => U,\n onErr: (error: E) => F,\n ): (result: Result<T, E>) => Result<U, F>;\n} = dual(\n 3,\n <T, U, E, F>(\n result: Result<T, E>,\n onOk: (value: T) => U,\n onErr: (error: E) => F,\n ): Result<U, F> => (isOk(result) ? mkOk(onOk(result.value)) : mkErr(onErr(result.error))),\n);\n\n/**\n * Canonical bind (Fantasy Land `chain`). Chains a Result-returning step,\n * widening the error union to `E | F`. Short-circuits on `Err`.\n *\n * @example\n * ```ts\n * flatMap(parseInput(raw), (data) =>\n * data.id != null ? ok(data) : err({ kind: \"missing_id\" as const }),\n * );\n * // Result<Data, ParseError | { kind: \"missing_id\" }>\n * ```\n */\nexport const flatMap: {\n <T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F>;\n <T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: Result<T, E>) => Result<U, E | F>;\n} = dual(\n 2,\n <T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F> =>\n isOk(result) ? fn(result.value) : mkErr(result.error),\n);\n\n/**\n * Error-track bind — runs `fn` only when the result is `Err`, allowing\n * a failed workflow to recover to `Ok` or remap the failure. Mirror of\n * {@link flatMap} on the error channel.\n *\n * @example\n * ```ts\n * recover(networkResult, (e) =>\n * e.kind === \"rate_limit\" ? ok(cachedBody) : err(e),\n * );\n * ```\n */\nexport const recover: {\n <T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;\n <T, E, F>(fn: (error: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;\n} = dual(\n 2,\n <T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F> =>\n isErr(result) ? fn(result.error) : mkOk(result.value),\n);\n\n/**\n * Observe the `Ok` value for side effects (logging, metrics) without\n * changing the carried value. Passes `Err` through untouched.\n *\n * @example\n * ```ts\n * pipe(\n * parseConfig(raw),\n * tap((cfg) => log.info({ msg: \"parsed\", name: cfg.name })),\n * flatMap(validate),\n * );\n * ```\n */\nexport const tap: {\n <T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;\n <T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;\n} = dual(2, <T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E> => {\n if (isOk(result)) fn(result.value);\n return result;\n});\n\n/**\n * Observe the `Err` value for side effects (logging, metrics) without\n * changing the carried error. Passes `Ok` through untouched.\n *\n * @example\n * ```ts\n * pipe(\n * loadUser(id),\n * tapErr((e) => metrics.inc(\"user.load.fail\", { kind: e.kind })),\n * );\n * ```\n */\nexport const tapErr: {\n <T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;\n <E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;\n} = dual(2, <T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E> => {\n if (isErr(result)) fn(result.error);\n return result;\n});\n\n/**\n * Terminal collapse — fold both tracks into a single value. Dual-form:\n * 3-args data-first, 2-args curried for {@link pipe}. Returns whatever\n * the handlers return.\n *\n * For files that also import `match` from `ts-pattern`, use a namespace\n * import (`import * as R from \"@onrails/result\"` → `R.match`) to dissolve\n * the collision.\n *\n * @example\n * ```ts\n * const html = match(parsed, (cfg) => render(cfg), (e) => renderError(e));\n * ```\n */\nexport const match: {\n <T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;\n <T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U): (result: Result<T, E>) => U;\n} = dual(\n 3,\n <T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U =>\n isOk(result) ? onOk(result.value) : onErr(result.error),\n);\n\n/**\n * Returns the `Ok` value, or `defaultValue` when the result is `Err`. The\n * fallback may be a different type from `T` — the return widens to `T | U`,\n * matching `ResultAsync.unwrapOr` and the compat shim.\n *\n * @example\n * ```ts\n * unwrapOr(parsedSetting, \"default-value\"); // string\n * unwrapOr(parsedPort, null); // number | null\n * ```\n */\nexport const unwrapOr: {\n <T, E, U>(result: Result<T, E>, defaultValue: U): T | U;\n <U>(defaultValue: U): <T, E>(result: Result<T, E>) => T | U;\n} = dual(2, <T, E, U>(result: Result<T, E>, defaultValue: U): T | U =>\n isOk(result) ? result.value : defaultValue,\n);\n\n/**\n * Test/assert helper — returns the `Ok` value, or **throws the original `Err`\n * value** when called on an `Err`. This is the assertion tier (RFC 0001 §4):\n * intended for `*.spec.ts` / `*.test.ts`, where throwing fails the test loudly.\n * In business logic prefer {@link match} or {@link unwrapOr}; the lint plugins\n * flag `unwrapOk` outside test files.\n *\n * @returns the unwrapped `Ok` value\n * @throws the carried `Err` value when the result is `Err`\n *\n * @example\n * ```ts\n * // in a *.spec.ts\n * expect(unwrapOk(ok(5))).toBe(5);\n * expect(() => unwrapOk(err(error))).toThrow(error);\n * ```\n */\nexport function unwrapOk<T, E>(result: Result<T, E>): T {\n if (isErr(result)) throw result.error;\n return result.value;\n}\n\n/** Alias of {@link unwrapOk} for syntax cohesion with `@onrails/maybe`. */\nexport const unwrap = unwrapOk;\n\n/**\n * Test/assert helper — returns the `Err` value, or **throws a `TypeError`**\n * when called on an `Ok`. Mirror of {@link unwrapOk} on the error track and\n * part of the same assertion tier (RFC 0001 §4): intended for `*.spec.ts` /\n * `*.test.ts`. Prefer {@link match} / {@link unwrapOr} in business logic; the\n * lint plugins flag `unwrapErr` outside test files.\n *\n * @returns the unwrapped `Err` value\n * @throws `TypeError` when the result is `Ok`\n *\n * @example\n * ```ts\n * // in a *.spec.ts\n * expect(unwrapErr(err(\"x\"))).toBe(\"x\");\n * expect(() => unwrapErr(ok(5))).toThrow(TypeError);\n * ```\n */\nexport function unwrapErr<T, E>(result: Result<T, E>): E {\n if (isOk(result)) throw new TypeError(\"unwrapErr called on Ok\");\n return result.error;\n}\n\n/**\n * Wraps a throwing sync **function**, returning a function that produces a\n * {@link Result} instead of throwing. Thrown errors pass through `onThrow` to\n * become a typed `Err`; a normal return becomes `Ok`. Same name and shape as\n * neverthrow's `Result.fromThrowable`.\n *\n * Reach for {@link trySync} instead when you want to run the call right now\n * rather than build a reusable wrapper.\n *\n * @param fn - the throwing function to wrap\n * @param onThrow - maps a thrown value to the `Err` channel\n * @returns a function with `fn`'s parameters that returns `Result<ReturnType, E>`\n *\n * @example\n * ```ts\n * type ParseError = { kind: \"parse\"; message: string };\n * const parse = fromThrowable(\n * JSON.parse,\n * (e): ParseError => ({ kind: \"parse\", message: String(e) }),\n * );\n * parse(\"{}\"); // Ok({})\n * parse(\"nope\"); // Err({ kind: \"parse\", … })\n * ```\n */\nexport function fromThrowable<A extends readonly unknown[], T, E>(\n fn: (...args: A) => T,\n onThrow: (error: unknown) => E,\n): (...args: A) => Result<T, E>;\nexport function fromThrowable<F extends (...args: never) => unknown, E>(\n fn: F,\n onThrow: (error: unknown) => E,\n): (...args: Parameters<F>) => Result<ReturnType<F>, E>;\nexport function fromThrowable(\n fn: (...args: never) => unknown,\n onThrow: (error: unknown) => unknown,\n): (...args: never) => Result<unknown, unknown> {\n return (...args: never) => {\n try {\n return mkOk(fn(...args));\n } catch (error) {\n return mkErr(onThrow(error));\n }\n };\n}\n\n/**\n * Runs `fn` **now**, catching a throw into the `Err` track — the sync mirror\n * of `tryAsync`. Without `onThrow` the thrown value is normalized to an\n * `Error`; pass a mapper for a typed error.\n *\n * To lift a throwing function once and call it many times, use\n * {@link fromThrowable}.\n *\n * @param fn - the throwing thunk to run\n * @param onThrow - maps a thrown value to the `Err` channel\n *\n * @example\n * ```ts\n * trySync(() => JSON.parse(raw)); // Result<unknown, Error>\n * trySync(() => Schema.parse(input), (e): SchemaError => ({\n * kind: \"schema\",\n * cause: e,\n * }));\n * ```\n */\nexport function trySync<T>(fn: () => T): Result<T, Error>;\nexport function trySync<T, E>(fn: () => T, onThrow: (error: unknown) => E): Result<T, E>;\nexport function trySync<T, E>(fn: () => T, onThrow?: (error: unknown) => E): Result<T, E | Error> {\n try {\n return mkOk(fn());\n } catch (error) {\n return mkErr(onThrow ? onThrow(error) : toError(error));\n }\n}\n\nconst printPayload = (payload: unknown): string => {\n try {\n return JSON.stringify(payload) ?? String(payload);\n } catch {\n // cyclic or non-JSON payload — a debug printer must not throw\n return String(payload);\n }\n};\n\n/**\n * Debug printer — renders a result as `Ok(…)` / `Err(…)` for logs. Payloads\n * print as JSON (values are plain data by design); non-JSON payloads fall\n * back to `String(...)`.\n *\n * @example\n * ```ts\n * show(ok(1)); // 'Ok(1)'\n * show(err({ kind: \"e\" })); // 'Err({\"kind\":\"e\"})'\n * ```\n */\nexport const show = <T, E>(result: Result<T, E>): string =>\n isOk(result) ? `Ok(${printPayload(result.value)})` : `Err(${printPayload(result.error)})`;\n","/**\n * The core railway type: a tagged union carrying either a success value\n * (`Ok<T>`) or a failure value (`Err<E>`). It is a plain discriminated union\n * on `_tag` — no classes — so it is immutable and tree-shake friendly. Narrow\n * with {@link isOk} / {@link isErr} to read `.value` / `.error`.\n *\n * @typeParam T - the `Ok` value type\n * @typeParam E - the `Err` error type\n *\n * @example\n * ```ts\n * function parse(raw: string): Result<number, \"nan\"> {\n * const n = Number(raw);\n * return Number.isNaN(n) ? err(\"nan\") : ok(n);\n * }\n * ```\n */\nexport type Result<T, E> =\n | { readonly _tag: \"Ok\"; readonly value: T }\n | { readonly _tag: \"Err\"; readonly error: E };\n\n/**\n * The success branch of a {@link Result} — `{ _tag: \"Ok\"; value: T }`.\n * The `E` parameter keeps the type assignable to `Result<T, E>`.\n */\nexport type Ok<T, E = never> = Extract<Result<T, E>, { _tag: \"Ok\" }>;\n\n/**\n * The failure branch of a {@link Result} — `{ _tag: \"Err\"; error: E }`.\n */\nexport type Err<T, E> = Extract<Result<T, E>, { _tag: \"Err\" }>;\n\n/**\n * Error subclass representing an **unexpected** defect — a thrown exception or\n * promise rejection that was not part of the modelled `Err` union. Used as the\n * default mapping when {@link fromAsync} is called without an `onDefect`\n * handler; the original cause is preserved on `.cause`.\n *\n * @example\n * ```ts\n * const defect = new UnexpectedError(\"Unexpected async defect\", caught);\n * defect.cause; // the original thrown value\n * ```\n */\nexport class UnexpectedError extends Error {\n constructor(\n message: string,\n override readonly cause?: unknown,\n ) {\n super(message);\n this.name = \"UnexpectedError\";\n }\n}\n","import { mkErr, mkOk } from \"./internal/ctor.js\";\nimport type { InferErr, InferOk } from \"./internal/infer.js\";\nimport type { Payload } from \"./internal/payload.js\";\nimport { bimap, isErr, map, mapErr } from \"./result.js\";\nimport type { Result } from \"./types.js\";\nimport { UnexpectedError } from \"./types.js\";\n\ntype PromiseFactory<T, E> = () => Promise<Result<T, E>>;\ntype AnyResultAsync = ResultAsync<unknown, unknown>;\n\n/**\n * Mirrors the sync `CombineTuple` in `./collections.ts`: the element check\n * lives in the RETURN type, never in the parameter constraint. A\n * `ResultAsync<unknown, unknown>` constraint contextually types the arguments\n * and widens `okAsync(1)`'s defaulted `E = never` to `unknown`, so call sites\n * would need hoisted intermediates to keep their error union.\n */\ntype CombineTupleAsync<R extends readonly unknown[]> = R[number] extends AnyResultAsync\n ? ResultAsync<{ [K in keyof R]: InferOk<R[K]> }, InferErr<R[number]>>\n : never;\n\n/** Mirrors `TupleGuard` in `./collections.ts` — see the note there. */\ntype AsyncTupleGuard<R extends readonly unknown[]> = R[number] extends AnyResultAsync\n ? []\n : [error: \"expected an array of ResultAsync values\"];\n\n/** Collapses settled results left-to-right, short-circuiting on the first `Err`. */\nconst sequenceSettled = (\n settled: readonly Result<unknown, unknown>[],\n): Result<unknown[], unknown> => {\n const values: unknown[] = [];\n for (const result of settled) {\n if (isErr(result)) {\n return mkErr(result.error);\n }\n values.push(result.value);\n }\n return mkOk(values);\n};\n\n/**\n * Async railway carrier. Wraps a deferred `Promise<Result<T, E>>` and exposes\n * the same dual-track transforms as the sync {@link Result} — `map`, `mapErr`,\n * `bimap`, `flatMap`, `recover`, `tap`, `match`. The public API never surfaces `Promise<Result<…>>`\n * directly: `await` the instance (it is thenable) or call {@link resolve} /\n * {@link match} to settle it.\n *\n * Construct via the static factories ({@link ok}, {@link err}, {@link fromPromise},\n * {@link defer}, …) rather than `new` — the constructor is `protected`.\n *\n * @typeParam T - the `Ok` value type\n * @typeParam E - the `Err` error type\n *\n * @example\n * ```ts\n * const user = ResultAsync.fromPromise(api.getUser(id), toAppError)\n * .map((u) => u.profile)\n * .recover(() => ResultAsync.ok(guestProfile));\n *\n * const r = await user; // Result<Profile, AppError>\n * ```\n */\nexport class ResultAsync<T, E> {\n private promise: Promise<Result<T, E>> | null = null;\n\n protected constructor(protected readonly run: PromiseFactory<T, E>) {}\n\n /**\n * Lifts an already-settled sync {@link Result} into a {@link ResultAsync}.\n *\n * @example\n * ```ts\n * const ra = ResultAsync.fromResult(ok(42)); // ResultAsync<number, never>\n * ```\n */\n static fromResult<T, E>(result: Result<T, E>): ResultAsync<T, E> {\n return new ResultAsync(async () => result);\n }\n\n /**\n * Wraps a `PromiseLike<T>` that may reject. Rejections pass through `onReject`\n * to become a typed `Err`; resolution becomes `Ok<T>`.\n *\n * @param promise - the promise to wrap\n * @param onReject - maps a rejection reason to the `Err` channel\n *\n * @example\n * ```ts\n * const body = ResultAsync.fromPromise(\n * fetch(url).then((r) => r.text()),\n * (e): NetError => ({ kind: \"net\", cause: String(e) }),\n * );\n * ```\n */\n static fromPromise<T, E>(\n promise: PromiseLike<T>,\n onReject: (error: unknown) => E,\n ): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n try {\n return mkOk(await promise);\n } catch (error) {\n return mkErr(onReject(error));\n }\n });\n }\n\n /**\n * Wraps a `PromiseLike<T>` that is **guaranteed not to reject**, skipping the\n * `onReject` mapper. Use only when rejection is provably impossible.\n *\n * @example\n * ```ts\n * const now = ResultAsync.fromSafePromise(Promise.resolve(Date.now()));\n * ```\n */\n static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E> {\n return new ResultAsync(async () => mkOk(await promise));\n }\n\n /**\n * Defers work until {@link resolve}. Unlike {@link fromPromise}, the factory\n * does not run until the `ResultAsync` is resolved (e.g. by `combineTuple` /\n * `combineTupleParallel`). The factory runs at most once — `resolve` memoizes.\n *\n * @example\n * ```ts\n * const insert = ResultAsync.defer(() => db.orders.insert(row));\n * // nothing has run yet\n * const r = await insert; // factory runs exactly once here\n * ```\n */\n static defer<T, E>(fn: () => Promise<Result<T, E>>): ResultAsync<T, E> {\n return new ResultAsync(fn);\n }\n\n /**\n * Lifts a value into an `Ok` async result.\n *\n * @example\n * ```ts\n * const r = ResultAsync.ok(42); // ResultAsync<number, never>\n * ```\n */\n static ok<const T extends Payload[]>(value: T): ResultAsync<T, never>;\n static ok<const T>(value: T): ResultAsync<T, never>;\n static ok<T, E>(value: T): ResultAsync<T, E>;\n static ok<T, E = never>(value: T): ResultAsync<T, E> {\n return new ResultAsync(async () => mkOk(value));\n }\n\n /**\n * Fantasy Land `pure` — alias of {@link ResultAsync.ok}. One lift name\n * shared across the trio (`of` / `Maybe.of` / `ResultAsync.of`).\n */\n static of<const T extends Payload[]>(value: T): ResultAsync<T, never>;\n static of<const T>(value: T): ResultAsync<T, never>;\n static of<T, E>(value: T): ResultAsync<T, E>;\n static of<T, E = never>(value: T): ResultAsync<T, E> {\n return ResultAsync.ok<T, E>(value);\n }\n\n /**\n * Lifts an error into an `Err` async result.\n *\n * @example\n * ```ts\n * const r = ResultAsync.err({ kind: \"not_found\" as const });\n * ```\n */\n static err<const E extends Payload[]>(error: E): ResultAsync<never, E>;\n static err<const E>(error: E): ResultAsync<never, E>;\n static err<T, E>(error: E): ResultAsync<T, E>;\n static err<T = never, E = unknown>(error: E): ResultAsync<T, E> {\n return new ResultAsync(async () => mkErr(error));\n }\n\n /**\n * Lifts an existing `Promise<Result<T, E>>` (e.g. from interop code) into a\n * {@link ResultAsync}. A thrown defect is routed through `onDefect`,\n * defaulting to {@link UnexpectedError}. The `Ok` value passes through\n * verbatim — even when it happens to be Result-shaped.\n *\n * @param promise - a promise that already yields a `Result`\n * @param onDefect - maps an unexpected throw to the `Err` channel\n * @see {@link fromAsync} from `@onrails/result`\n */\n static fromResultPromise<T, E>(\n promise: Promise<Result<T, E>>,\n onDefect?: (error: unknown) => E | UnexpectedError,\n ): ResultAsync<T, E | UnexpectedError> {\n const mapDefect =\n onDefect ?? ((error: unknown) => new UnexpectedError(\"Unexpected async defect\", error));\n return new ResultAsync<T, E | UnexpectedError>(async () => {\n try {\n return await promise;\n } catch (error) {\n return mkErr(mapDefect(error));\n }\n });\n }\n\n /**\n * Combines a homogeneous array of async results into one. Resolves\n * **sequentially** in input order, short-circuiting on the first `Err`.\n * For heterogeneous tuples that preserve per-index types, use\n * {@link combineTuple}; for wall-clock overlap, {@link combineTupleParallel}.\n *\n * @example\n * ```ts\n * const all = ResultAsync.combine([loadA(), loadB(), loadC()]);\n * // ResultAsync<Item[], LoadError>\n * ```\n */\n static combine<T, E>(results: readonly ResultAsync<T, E>[]): ResultAsync<T[], E> {\n return new ResultAsync(async () => {\n const values: T[] = [];\n for (const ra of results) {\n const result = await ra.resolve();\n if (isErr(result)) {\n return mkErr(result.error);\n }\n values.push(result.value);\n }\n return mkOk(values);\n });\n }\n\n /**\n * Heterogeneous async tuple combine — resolves branches **sequentially**\n * (left-to-right), returning the first `Err` in input order. Preserves each\n * branch's `Ok` type by position, so the result destructures type-safely.\n * This is the canonical sequential async combine (replaces the former\n * `sequenceTupleAsync`).\n *\n * @example\n * ```ts\n * const combined = ResultAsync.combineTuple([loadCfg(), loadCatalog()]);\n * // ResultAsync<readonly [Cfg, Catalog], CfgError | CatalogError>\n * const r = await combined;\n * if (isOk(r)) {\n * const [cfg, catalog] = r.value; // typed per position\n * }\n * ```\n */\n static combineTuple<const R extends readonly unknown[]>(\n results: R,\n ..._guard: AsyncTupleGuard<R>\n ): CombineTupleAsync<R> {\n // Runtime identical to combine; the cast restores per-index tuple types.\n return ResultAsync.combine(\n results as readonly AnyResultAsync[],\n ) as unknown as CombineTupleAsync<R>;\n }\n\n /**\n * Like {@link combineTuple}, but starts every branch before awaiting (wall-clock\n * parallel for independent IO). On failure, returns the first `Err` in input order.\n *\n * @example\n * ```ts\n * // independent IO — overlap them\n * const combined = ResultAsync.combineTupleParallel([\n * loadProfile(id),\n * loadMetrics(id),\n * ]);\n * ```\n */\n static combineTupleParallel<const R extends readonly unknown[]>(\n results: R,\n ..._guard: AsyncTupleGuard<R>\n ): CombineTupleAsync<R> {\n // Cast restores per-index tuple types over the untyped sequence core.\n return new ResultAsync(async () =>\n sequenceSettled(\n await Promise.all((results as readonly AnyResultAsync[]).map((ra) => ra.resolve())),\n ),\n ) as unknown as CombineTupleAsync<R>;\n }\n\n /**\n * Transforms the `Ok` value, passing `Err` through unchanged.\n *\n * @example\n * ```ts\n * ResultAsync.ok(2).map((n) => n * 3); // ResultAsync<number> → Ok 6\n * ```\n */\n map<U>(fn: (value: T) => U): ResultAsync<U, E> {\n return new ResultAsync(async () => map(await this.resolve(), fn));\n }\n\n /**\n * Transforms the `Err` value, passing `Ok` through unchanged — useful for\n * unifying heterogeneous failures into one app-level union.\n *\n * @example\n * ```ts\n * load(id).mapErr((e): AppError => ({ kind: \"load\", cause: e })); // ResultAsync<T, AppError>\n * ```\n */\n mapErr<F>(fn: (error: E) => F): ResultAsync<T, F> {\n return new ResultAsync(async () => mapErr(await this.resolve(), fn));\n }\n\n /**\n * Transforms both tracks in one step — the async mirror of the sync\n * `bimap`. Exactly one of `onOk` / `onErr` runs.\n *\n * @example\n * ```ts\n * load(id).bimap(toDto, (e): AppError => ({ kind: \"load\", cause: e }));\n * ```\n */\n bimap<U, F>(onOk: (value: T) => U, onErr: (error: E) => F): ResultAsync<U, F> {\n return new ResultAsync(async () => bimap(await this.resolve(), onOk, onErr));\n }\n\n /**\n * Canonical bind — chains a step that itself returns a `ResultAsync` or sync\n * `Result`, short-circuiting on `Err`. Error types accumulate (`E | F`).\n *\n * @example\n * ```ts\n * authenticate(req)\n * .flatMap((user) => loadProfile(user.id)) // ResultAsync\n * .flatMap((p) => validate(p)); // sync Result also accepted\n * ```\n */\n flatMap<U, F = E>(fn: (value: T) => ResultAsync<U, F> | Result<U, F>): ResultAsync<U, E | F> {\n return new ResultAsync<U, E | F>(async () => {\n const first = await this.resolve();\n if (isErr(first)) {\n return first;\n }\n const next = fn(first.value);\n return next instanceof ResultAsync ? next.resolve() : next;\n });\n }\n\n /**\n * neverthrow-compat alias of {@link flatMap}. Kept as the documented compat\n * tier; prefer {@link flatMap} in new code.\n *\n * @example\n * ```ts\n * authenticate(req).andThen((user) => loadProfile(user.id));\n * ```\n */\n andThen<U, F = E>(fn: (value: T) => ResultAsync<U, F> | Result<U, F>): ResultAsync<U, E | F> {\n return this.flatMap(fn);\n }\n\n /**\n * Error-channel bind — runs `fn` only on `Err`, swapping in a recovery\n * `ResultAsync` or sync `Result`. `Ok` passes through. The mirror of\n * {@link flatMap} on the error track.\n *\n * @example\n * ```ts\n * loadFromCache(id).recover(() => loadFromOrigin(id)); // ResultAsync<T, F>\n * ```\n */\n recover<F>(fn: (error: E) => ResultAsync<T, F> | Result<T, F>): ResultAsync<T, F> {\n return new ResultAsync<T, F>(async () => {\n const first = await this.resolve();\n if (!isErr(first)) {\n return first;\n }\n const next = fn(first.error);\n return next instanceof ResultAsync ? next.resolve() : next;\n });\n }\n\n /**\n * neverthrow-compat alias of {@link recover}. Kept as the documented compat\n * tier; prefer {@link recover} in new code.\n *\n * @example\n * ```ts\n * loadFromCache(id).orElse(() => loadFromOrigin(id));\n * ```\n */\n orElse<F>(fn: (error: E) => ResultAsync<T, F> | Result<T, F>): ResultAsync<T, F> {\n return this.recover(fn);\n }\n\n /**\n * Runs a side effect on the `Ok` value and passes the result through\n * unchanged; a no-op on `Err`. Mirrors {@link tapErr}.\n *\n * @example\n * ```ts\n * saveUser(u).tap((saved) => analytics.track(\"user_saved\", saved.id));\n * ```\n */\n tap(fn: (value: T) => void): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n const result = await this.resolve();\n if (!isErr(result)) {\n fn(result.value);\n }\n return result;\n });\n }\n\n /**\n * Runs a side effect on the `Err` value and passes the result through\n * unchanged; a no-op on `Ok`. The error-track mirror of {@link tap}.\n *\n * @example\n * ```ts\n * saveUser(u).tapErr((e) => logger.warn(\"save failed\", e));\n * ```\n */\n tapErr(fn: (error: E) => void): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n const result = await this.resolve();\n if (isErr(result)) {\n fn(result.error);\n }\n return result;\n });\n }\n\n /**\n * Settles to the `Ok` value, or `defaultValue` if `Err`. Terminal — returns a\n * plain `Promise`, not a `ResultAsync`.\n *\n * @example\n * ```ts\n * const profile = await loadProfile(id).unwrapOr(guestProfile);\n * ```\n */\n unwrapOr<U>(defaultValue: U): Promise<T | U> {\n return this.resolve().then((result) => (isErr(result) ? defaultValue : result.value));\n }\n\n /**\n * Terminal collapse — folds both tracks into a single awaited value. Returns\n * a plain `Promise`, settling the carrier exactly once.\n *\n * @param onOk - handles the `Ok` value\n * @param onErr - handles the `Err` value\n *\n * @example\n * ```ts\n * const status = await save(row).match(\n * () => 200,\n * (e) => (e.kind === \"conflict\" ? 409 : 500),\n * );\n * ```\n */\n match<U1, U2 = U1>(onOk: (value: T) => U1, onErr: (error: E) => U2): Promise<U1 | U2> {\n return this.resolve().then((result) =>\n isErr(result) ? onErr(result.error) : onOk(result.value),\n );\n }\n\n /**\n * Settles the carrier to a bare sync {@link Result}, memoizing so the\n * underlying factory runs at most once. Prefer `await ra` (the thenable) or\n * {@link match} in app code; use `resolve` when you need the tagged union back.\n *\n * @example\n * ```ts\n * const r = await load(id).resolve(); // Result<Data, LoadError>\n * if (isOk(r)) use(r.value);\n * ```\n */\n resolve(): Promise<Result<T, E>> {\n if (!this.promise) {\n this.promise = this.run();\n }\n return this.promise;\n }\n\n /**\n * Thenable shim — `await ra` resolves to a bare tagged-union `Result<T, E>`.\n * Narrow with `isOk(r)` / `isErr(r)` to read `.value` / `.error`.\n */\n // biome-ignore lint/suspicious/noThenProperty: makes ResultAsync awaitable\n then<R1 = Result<T, E>, R2 = never>(\n onfulfilled?: ((value: Result<T, E>) => R1 | PromiseLike<R1>) | undefined | null,\n onrejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | undefined | null,\n ): Promise<R1 | R2> {\n return this.resolve().then(\n // Safe: without onfulfilled, R1 stays at its Result<T, E> default.\n (r) => (onfulfilled ? onfulfilled(r) : (r as unknown as R1)),\n onrejected ?? undefined,\n );\n }\n}\n\n/**\n * The free-function lift helpers (`okAsync`, `errAsync`, `fromPromise`,\n * `fromSafePromise`, `fromResult`, `fromAsync`, `asyncAfter`, `tryAsync`)\n * live in `./async-lift.ts` and are re-exported from the package index.\n */\n","import { ResultAsync } from \"./async.js\";\nimport { mkErr, mkOk } from \"./internal/ctor.js\";\nimport { flatMap, fromThrowable, map } from \"./result.js\";\nimport type { Result } from \"./types.js\";\n\n/**\n * Tracks whether a {@link Railway} workflow has crossed an async boundary.\n * Sync workflows return {@link Result}; async workflows return {@link ResultAsync}.\n */\nexport type RailwayMode = \"sync\" | \"async\";\n\n/**\n * Mode-aware output type for a {@link Railway} workflow: sync mode →\n * `Result<T, E>`, async mode → `ResultAsync<T, E>`.\n */\nexport type RailwayOutput<T, E, M extends RailwayMode> = M extends \"async\"\n ? ResultAsync<T, E>\n : Result<T, E>;\n\n/**\n * Runtime carrier for a workflow. Invariant: the `mode` tag mirrors the\n * phantom `M` on {@link Railway} — every construction site writes matching\n * tag and type parameter, and {@link Railway.step} / {@link Railway.out}\n * are the only places that re-link the two.\n */\ntype RailwayState<C extends object, E> =\n | { readonly mode: \"sync\"; readonly result: Result<C, E> }\n | { readonly mode: \"async\"; readonly result: ResultAsync<C, E> };\n\nexport type BranchFn<C extends object> = (ctx: C) => ResultAsync<unknown, unknown>;\nexport type BranchRecord = Record<string, (ctx: never) => ResultAsync<unknown, unknown>>;\ntype BranchOk<R> = R extends (ctx: never) => ResultAsync<infer T, infer _E> ? T : never;\ntype BranchErr<R> = R extends (ctx: never) => ResultAsync<infer _T, infer E> ? E : never;\nexport type ParallelOutput<R extends BranchRecord> = {\n [K in keyof R]: BranchOk<R[K]>;\n};\nexport type ParallelError<R extends BranchRecord> = BranchErr<R[keyof R]>;\n\n/** Collapses an intersection into a single displayed object type. */\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\n/**\n * The context after one more named step. Chaining `C & Record<K, T>` instead\n * builds an intersection that TS echoes verbatim in every hover and error\n * message (`Record<never, never> & Record<\"id\", string> & …`); `Omit` keeps\n * property modifiers and `Prettify` collapses the result to a flat object.\n */\ntype Extend<C extends object, K extends string, T> = Prettify<Omit<C, K> & Record<K, T>>;\n\n/**\n * Rejects a key already present in the context. Railway merges each step with\n * a spread, so a repeated key overwrites at runtime while the old\n * `C & Record<K, T>` type intersected the two field types — `number & string`\n * became `never` and nothing complained. Resolves to `[]` for a fresh key and\n * to an unsatisfiable extra parameter otherwise, so the collision fails at the\n * call site with the offending name in the message.\n */\ntype FreshKey<C, K extends string> = K extends keyof C\n ? [error: `key \"${K}\" already exists in the workflow context`]\n : [];\n\n/** {@link FreshKey} for a whole record of parallel branches. */\ntype FreshKeys<C, R> =\n Extract<keyof R, keyof C> extends never\n ? []\n : [\n error: `branch key \"${Extract<keyof R, keyof C> & string}\" already exists in the workflow context`,\n ];\n\nconst addField = <C extends object, K extends string, T>(\n ctx: C,\n key: K,\n value: T,\n): Extend<C, K, T> => ({ ...ctx, [key]: value }) as Extend<C, K, T>;\n\n/**\n * Named-context workflow builder. Each step appends a typed field to the\n * accumulating context object; the workflow tracks sync/async mode so the\n * final output type ({@link RailwayOutput}) is correct.\n */\nexport class Railway<C extends object, E, M extends RailwayMode> {\n private constructor(private readonly state: RailwayState<C, E>) {}\n\n /** Start an empty sync workflow with no fields in context. */\n static empty(): Railway<Record<never, never>, never, \"sync\"> {\n return new Railway({ mode: \"sync\", result: mkOk({}) });\n }\n\n /** Start a sync workflow with the given context as the initial state. */\n static context<C extends object>(context: C): Railway<C, never, \"sync\"> {\n return new Railway({ mode: \"sync\", result: mkOk(context) });\n }\n\n /** Start a sync workflow with a throwing function. */\n static fromSync<K extends string, T, E>(\n key: K,\n fn: () => T,\n onThrow: (error: unknown) => E,\n ): Railway<Record<K, T>, E, \"sync\"> {\n return Railway.empty().addSync(key, () => fromThrowable(fn, onThrow)()) as Railway<\n Record<K, T>,\n E,\n \"sync\"\n >;\n }\n\n /** Start a sync workflow with a `Result`-returning function. */\n static fromResult<K extends string, T, E>(\n key: K,\n fn: () => Result<T, E>,\n ): Railway<Record<K, T>, E, \"sync\"> {\n return Railway.empty().addSync(key, fn) as Railway<Record<K, T>, E, \"sync\">;\n }\n\n /** Start an async workflow with a `PromiseLike`-returning function. */\n static fromPromise<K extends string, T, E>(\n key: K,\n fn: () => PromiseLike<T>,\n onReject: (error: unknown) => E,\n ): Railway<Record<K, T>, E, \"async\"> {\n return Railway.empty().addAsync(key, () => ResultAsync.fromPromise(fn(), onReject)) as Railway<\n Record<K, T>,\n E,\n \"async\"\n >;\n }\n\n /** Start an async workflow with a `ResultAsync`-returning function. */\n static fromAsync<K extends string, T, E>(\n key: K,\n fn: () => ResultAsync<T, E>,\n ): Railway<Record<K, T>, E, \"async\"> {\n return Railway.empty().addAsync(key, fn) as Railway<Record<K, T>, E, \"async\">;\n }\n\n /**\n * Rebuild the workflow in its current mode after transforming the carried\n * result. Single re-link point for the state/phantom invariant: the mode\n * tag is preserved verbatim, so `M` still describes the new state — TS\n * cannot narrow the phantom `M` from the runtime tag, hence the cast.\n */\n private step<C2 extends object, F>(\n onSync: (result: Result<C, E>) => Result<C2, E | F>,\n onAsync: (result: ResultAsync<C, E>) => ResultAsync<C2, E | F>,\n ): Railway<C2, E | F, M> {\n const next: RailwayState<C2, E | F> =\n this.state.mode === \"sync\"\n ? { mode: \"sync\", result: onSync(this.state.result) }\n : { mode: \"async\", result: onAsync(this.state.result) };\n return new Railway(next) as Railway<C2, E | F, M>;\n }\n\n /**\n * Project the carried result into the mode-aware output type. Counterpart\n * of {@link Railway.step} for terminal steps: the runtime branch matches\n * the branch `RailwayOutput` picks for `M` (state/phantom invariant), but\n * TS cannot resolve the conditional on an unresolved `M`, hence the cast.\n */\n private out<T>(\n onSync: (result: Result<C, E>) => Result<T, E>,\n onAsync: (result: ResultAsync<C, E>) => ResultAsync<T, E>,\n ): RailwayOutput<T, E, M> {\n return (\n this.state.mode === \"sync\" ? onSync(this.state.result) : onAsync(this.state.result)\n ) as RailwayOutput<T, E, M>;\n }\n\n /** Lift the carried result into `ResultAsync` for async-upgrading steps. */\n private toAsync(): ResultAsync<C, E> {\n return this.state.mode === \"sync\"\n ? ResultAsync.fromResult(this.state.result)\n : this.state.result;\n }\n\n /**\n * Internal plumbing for a sync field append. Public steps wrap this and add\n * the {@link FreshKey} collision guard; the static factories bypass the\n * guard because they always start from an empty context.\n */\n private addSync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => Result<T, F>,\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.step<Extend<C, K, T>, F>(\n (result) => flatMap(result, (ctx) => map(fn(ctx), (value) => addField(ctx, key, value))),\n (result) =>\n result.flatMap((ctx) =>\n ResultAsync.fromResult(fn(ctx)).map((value) => addField(ctx, key, value)),\n ),\n );\n }\n\n /** Internal plumbing for an async field append — upgrades the mode. */\n private addAsync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => ResultAsync<T, F>,\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return new Railway({\n mode: \"async\",\n result: this.toAsync().flatMap((ctx) => fn(ctx).map((value) => addField(ctx, key, value))),\n });\n }\n\n /** Pure sync derivation. */\n derive<K extends string, T>(\n key: K,\n fn: (ctx: C) => T,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E, M> {\n return this.addSync(key, (ctx) => mkOk(fn(ctx)));\n }\n\n /** Throwing sync transform. */\n fromSync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => T,\n onThrow: (error: unknown) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.addSync(key, (ctx) => fromThrowable(fn, onThrow)(ctx));\n }\n\n /** Sync `Result`-returning step. */\n fromResult<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => Result<T, F>,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.addSync(key, fn);\n }\n\n /** Promise-returning step — upgrades the workflow to async mode. */\n fromPromise<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => PromiseLike<T>,\n onReject: (error: unknown) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return this.addAsync(key, (ctx) => ResultAsync.fromPromise(fn(ctx), onReject));\n }\n\n /** `ResultAsync`-returning step — upgrades the workflow to async mode. */\n fromAsync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => ResultAsync<T, F>,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return this.addAsync(key, fn);\n }\n\n /** Narrow a nullable context field into a required non-null field. */\n require<K extends string, S extends keyof C, F>(\n key: K,\n source: S,\n onMissing: (ctx: C) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, NonNullable<C[S]>>, E | F, M> {\n return this.addSync(key, (ctx) => {\n const value = ctx[source];\n return value == null ? mkErr(onMissing(ctx)) : mkOk(value as NonNullable<C[S]>);\n });\n }\n\n /** Run independent `ResultAsync` branches concurrently and merge outputs. */\n parallel<R extends Record<string, BranchFn<C>>>(\n branches: R,\n ..._guard: FreshKeys<C, R>\n ): Railway<Prettify<Omit<C, keyof R> & ParallelOutput<R>>, E | ParallelError<R>, \"async\"> {\n const merged = this.toAsync().flatMap((ctx) =>\n ResultAsync.combineTupleParallel(\n Object.entries(branches).map(([, branch]) => branch(ctx)),\n ).map(\n (values) =>\n // Branch outputs land under exactly R's keys; Object.fromEntries\n // erases that correspondence, so reassert the merged shape.\n ({\n ...ctx,\n ...Object.fromEntries(Object.keys(branches).map((key, index) => [key, values[index]])),\n }) as Prettify<Omit<C, keyof R> & ParallelOutput<R>>,\n ),\n );\n return new Railway({\n mode: \"async\",\n // BranchFn erases per-key types to `ResultAsync<unknown, unknown>`\n // (ParallelOutput/ParallelError recover them from R), so re-assert the\n // precise value/error union the branches actually produce.\n result: merged as ResultAsync<\n Prettify<Omit<C, keyof R> & ParallelOutput<R>>,\n E | ParallelError<R>\n >,\n });\n }\n\n /** Project the final context into the workflow's output type. */\n select<T>(fn: (ctx: C) => T): RailwayOutput<T, E, M> {\n return this.out(\n (result) => map(result, fn),\n (result) => result.map(fn),\n );\n }\n\n /** Return the accumulated context as-is. */\n done(): RailwayOutput<C, E, M> {\n return this.out(\n (result) => result,\n (result) => result,\n );\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/internal/proto.ts","../src/internal/dual.ts","../src/result.ts","../src/types.ts","../src/async.ts","../src/railway.ts"],"names":[],"mappings":";AAMA,IAAM,OAAA,mBAAU,MAAA,CAAO,GAAA,CAAI,4BAA4B,CAAA;AAYvD,IAAM,MAAA,GAAN,MAAM,OAAA,CAA4C;AAAA,EACvC,IAAA;AAAA,EACA,KAAA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AAC,GAAG,MAAM,CAAA,KAAM,MAAA,CAAO,GAAG,CAAC;AAAA,OAC1D,CAAE,GAAA;AAAA,QACA,CAAC,MAAA;AAAA;AAAA;AAAA,UAGE;AAAA,YACC,GAAG,GAAA;AAAA,YACH,GAAG,MAAA,CAAO,WAAA,CAAY,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA,CAAE,GAAA,CAAI,CAAC,GAAA,EAAK,UAAU,CAAC,GAAA,EAAK,OAAO,KAAK,CAAC,CAAC,CAAC;AAAA;AACvF;AAAA;AACJ,KACF;AACA,IAAA,OAAO,IAAI,QAAA,CAAQ;AAAA,MACjB,IAAA,EAAM,OAAA;AAAA;AAAA;AAAA;AAAA,MAIN,MAAA,EAAQ;AAAA,KAIT,CAAA;AAAA,EACH;AAAA;AAAA,EAGA,OAAU,EAAA,EAA2C;AACnD,IAAA,OAAO,IAAA,CAAK,GAAA;AAAA,MACV,CAAC,MAAA,KAAW,GAAA,CAAI,MAAA,EAAQ,EAAE,CAAA;AAAA,MAC1B,CAAC,MAAA,KAAW,MAAA,CAAO,GAAA,CAAI,EAAE;AAAA,KAC3B;AAAA,EACF;AAAA;AAAA,EAGA,IAAA,GAA+B;AAC7B,IAAA,OAAO,IAAA,CAAK,GAAA;AAAA,MACV,CAAC,MAAA,KAAW,MAAA;AAAA,MACZ,CAAC,MAAA,KAAW;AAAA,KACd;AAAA,EACF;AACF","file":"railway.js","sourcesContent":["import type { Result, ResultMethods } from \"../types.js\";\n\n/**\n * `util.inspect` / `Bun.inspect` / `console.log` hook: render `Ok(1)` instead of dumping the class\n * name and every prototype method. Symbol-keyed, so it adds no enumerable field and JSON is unchanged.\n */\nconst INSPECT = Symbol.for(\"nodejs.util.inspect.custom\");\ntype Inspect = (value: unknown, options?: unknown) => string;\n\n/**\n * Live carrier classes. `_tag` and `value` / `error` are own enumerable\n * fields, so JSON and `structuredClone` see plain data; every method lives on\n * the prototype and is therefore non-enumerable.\n *\n * Internal only: reach carriers through {@link newOk} / {@link newErr}\n * (aliased as `mkOk` / `mkErr` for unlocked internals) or the public\n * `ok` / `err`.\n */\nclass OkImpl<T, E> implements ResultMethods<T, E> {\n readonly _tag: \"Ok\";\n readonly value: T;\n // explicit assignment order keeps `_tag` first, so JSON key order matches the plain-data shape\n constructor(value: T) {\n this._tag = \"Ok\";\n this.value = value;\n }\n [INSPECT](_depth: number, options: unknown, inspect: Inspect): string {\n return `Ok(${inspect(this.value, options)})`;\n }\n map<U>(fn: (value: T) => U): Result<U, E> {\n return new OkImpl<U, E>(fn(this.value));\n }\n mapErr<F>(_fn: (error: E) => F): Result<T, F> {\n return new OkImpl<T, F>(this.value);\n }\n flatMap<U, F>(fn: (value: T) => Result<U, F>): Result<U, E | F> {\n return fn(this.value);\n }\n andThen<U, F>(fn: (value: T) => Result<U, F>): Result<U, E | F> {\n return fn(this.value);\n }\n recover<F>(_fn: (error: E) => Result<T, F>): Result<T, F> {\n return new OkImpl<T, F>(this.value);\n }\n tap(fn: (value: T) => void): Result<T, E> {\n fn(this.value);\n return this;\n }\n tapErr(_fn: (error: E) => void): Result<T, E> {\n return this;\n }\n match<U>(onOk: (value: T) => U, _onErr: (error: E) => U): U {\n return onOk(this.value);\n }\n unwrapOr<U>(_defaultValue: U): T | U {\n return this.value;\n }\n}\n\nclass ErrImpl<T, E> implements ResultMethods<T, E> {\n readonly _tag: \"Err\";\n readonly error: E;\n constructor(error: E) {\n this._tag = \"Err\";\n this.error = error;\n }\n [INSPECT](_depth: number, options: unknown, inspect: Inspect): string {\n return `Err(${inspect(this.error, options)})`;\n }\n map<U>(_fn: (value: T) => U): Result<U, E> {\n return new ErrImpl<U, E>(this.error);\n }\n mapErr<F>(fn: (error: E) => F): Result<T, F> {\n return new ErrImpl<T, F>(fn(this.error));\n }\n flatMap<U, F>(_fn: (value: T) => Result<U, F>): Result<U, E | F> {\n return new ErrImpl<U, E | F>(this.error);\n }\n andThen<U, F>(_fn: (value: T) => Result<U, F>): Result<U, E | F> {\n return new ErrImpl<U, E | F>(this.error);\n }\n recover<F>(fn: (error: E) => Result<T, F>): Result<T, F> {\n return fn(this.error);\n }\n tap(_fn: (value: T) => void): Result<T, E> {\n return this;\n }\n tapErr(fn: (error: E) => void): Result<T, E> {\n fn(this.error);\n return this;\n }\n match<U>(_onOk: (value: T) => U, onErr: (error: E) => U): U {\n return onErr(this.error);\n }\n unwrapOr<U>(defaultValue: U): T | U {\n return defaultValue;\n }\n}\n\nexport const newOk = <T, E>(value: T): Result<T, E> => new OkImpl<T, E>(value);\nexport const newErr = <T, E>(error: E): Result<T, E> => new ErrImpl<T, E>(error);\n","/**\n * @onrails/result internal — owning module for the **dual-form idiom**.\n *\n * Every dual-form transform in this package (`map`, `flatMap`, `match`, …)\n * accepts either shape:\n *\n * data-first: `map(result, fn)` — full arity, runs immediately\n * curried: `map(fn)(result)` — arity minus the leading `self`, returns\n * a unary function awaiting the data\n *\n * {@link dual} is the single arity dispatcher those transforms derive from;\n * hand-rolled `args.length` checks are not allowed elsewhere.\n *\n * NOTE: a twin copy of this module lives at `packages/maybe/src/internal/dual.ts`\n * — keep the two in sync when editing.\n *\n * Internal only: not listed in package.json `exports` or tsup entries.\n */\n\n/**\n * Derive a dual-form (data-first / curried) function from a single\n * data-first `body`.\n *\n * When called with `args.length >= arity` the wrapper runs `body(...args)`\n * (data-first). With fewer arguments it returns `(self) => body(self, ...args)`\n * — the curried, data-last-`self` form used with `pipe`/`flow`.\n *\n * The public type is supplied entirely by the caller: annotate the exported\n * const with the exact overload set and let `F` be inferred from it.\n *\n * @example\n * ```ts\n * export const map: {\n * <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;\n * <T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;\n * } = dual(\n * 2,\n * <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> =>\n * isOk(result) ? ok(fn(result.value)) : err(result.error),\n * );\n * ```\n */\nexport const dual = <F>(arity: number, body: (...args: never[]) => unknown): F => {\n // `(...args: never[]) => unknown` is the top function type, so every typed\n // body is assignable to the parameter — but it cannot be invoked with\n // `unknown` arguments. Re-view it through the variadic shape the dispatcher\n // actually forwards; the constraint is that `body` only ever receives the\n // arguments the caller's overload annotation admitted.\n const run = body as (...args: readonly unknown[]) => unknown;\n const dispatcher = (...args: readonly unknown[]): unknown =>\n args.length >= arity ? run(...args) : (self: unknown) => run(self, ...args);\n // The caller's overload annotation on the exported const supplies the real\n // public type; internally the dispatcher stays `unknown`-typed.\n return dispatcher as F;\n};\n","import { mkErr, mkOk } from \"./internal/ctor.js\";\nimport { dual } from \"./internal/dual.js\";\nimport type { Payload } from \"./internal/payload.js\";\nimport { newErr, newOk } from \"./internal/proto.js\";\nimport { toError } from \"./internal/to-error.js\";\nimport type { Err, Ok, Result } from \"./types.js\";\n\nexport type { Err, Ok, Result } from \"./types.js\";\n\n/**\n * Lifts a value into the success track.\n *\n * @example\n * ```ts\n * const r = ok(42); // Result<42, never> — literal locked\n * const typed: Result<number, \"parse\"> = ok(1);\n * const lift = <T, E>(v: T): Result<T, E> => ok(v); // generic T passes through\n * ```\n *\n * @remarks\n * An inline literal keeps its narrow type instead of widening (`ok(\"a\")` is\n * `Result<\"a\", never>`, not `Result<string, never>`), and an array literal\n * stays a mutable tuple (`ok([1, 2])` satisfies `Result<number[], E>`). A\n * generic `T` always comes back as `T`. Passing both type arguments opts out\n * of locking entirely.\n */\nexport function ok<const T extends Payload[]>(value: T): Result<T, never>;\nexport function ok<const T>(value: T): Result<T, never>;\nexport function ok<T, E>(value: T): Result<T, E>;\nexport function ok<T>(value: T): Result<T, never> {\n // Direct carrier construct — skip the unlocked `mkOk` alias hop on the hot path.\n return newOk(value);\n}\n\n/**\n * Fantasy Land `pure` — alias of {@link ok}. One lift name shared across the\n * trio (`of` / `Maybe.of` / `ResultAsync.of`) for generic and FL-style code.\n *\n * @example\n * ```ts\n * const r = of(42); // Result<42, never> — identical to ok(42)\n * ```\n */\nexport const of = ok;\n\n/**\n * Lifts a value into the error track.\n *\n * @example\n * ```ts\n * const r = err({ kind: \"parse\", message: \"bad json\" });\n * // Result<never, { readonly kind: \"parse\"; readonly message: string }>\n *\n * type AppError = { kind: \"parse\" } | { kind: \"io\" };\n * err<AppError>({ kind: \"io\" }); // Result<never, AppError> — one arg names the error\n * err<number, AppError>({ kind: \"io\" }); // Result<number, AppError> — neverthrow's T, E order\n * ```\n *\n * @remarks\n * One explicit type argument names the **error** channel, which is what the\n * call site almost always means. Both channels still take neverthrow's `T, E`\n * order. Inference locks literals the same way as {@link ok}, and a generic\n * `E` always comes back as `E`.\n */\nexport function err<const E extends Payload[]>(error: E): Result<never, E>;\nexport function err<const E>(error: E): Result<never, E>;\nexport function err<T, E>(error: E): Result<T, E>;\nexport function err<E>(error: E): Result<never, E> {\n // Direct carrier construct — skip the unlocked `mkErr` alias hop on the hot path.\n return newErr(error);\n}\n\n/**\n * Type-narrowing predicate: returns `true` when the result is `Ok`.\n *\n * @example\n * ```ts\n * if (isOk(r)) {\n * console.log(r.value); // narrowed to Ok branch\n * }\n * ```\n */\nexport const isOk = <T, E>(result: Result<T, E>): result is Ok<T, E> => result._tag === \"Ok\";\n\n/**\n * Type-narrowing predicate: returns `true` when the result is `Err`.\n *\n * @example\n * ```ts\n * if (isErr(r)) {\n * metrics.inc(\"error\", { kind: r.error.kind });\n * }\n * ```\n */\nexport const isErr = <T, E>(result: Result<T, E>): result is Err<T, E> => result._tag === \"Err\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Dual-form helpers — each export accepts either shape:\n// data-first: `map(result, fn)`\n// curried: `map(fn)(result)`\n// Arity dispatch is derived from the internal `dual` combinator\n// (src/internal/dual.ts); the const annotation carries the public overloads.\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Transform the `Ok` value, passing `Err` through unchanged. Dual-form:\n * call data-first or curried (for use with {@link pipe}).\n *\n * @example\n * ```ts\n * map(ok(2), (n) => n * 3); // Ok 6 — data-first\n * pipe(ok(\"x\"), map((s) => s.length));// Ok 1 — curried\n * ```\n */\nexport const map: {\n <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;\n <T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;\n} = dual(\n 2,\n <T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> =>\n isOk(result) ? mkOk(fn(result.value)) : mkErr(result.error),\n);\n\n/**\n * Transform the `Err` value, passing `Ok` through unchanged. Useful for\n * unifying heterogeneous failure types into one app-level union.\n *\n * @example\n * ```ts\n * type AppError = { kind: \"http\"; status: number } | { kind: \"parse\" };\n * pipe(\n * fetchSync(url), // Result<Body, { status: number }>\n * mapErr((e): AppError => ({ kind: \"http\", status: e.status })),\n * );\n * ```\n */\nexport const mapErr: {\n <T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;\n <E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;\n} = dual(\n 2,\n <T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> =>\n isErr(result) ? mkErr(fn(result.error)) : mkOk(result.value),\n);\n\n/**\n * Transform both tracks at once — `Ok` via `onOk`, `Err` via `onErr`.\n * Equivalent to `mapErr(onErr)(map(onOk)(result))` but in one pass.\n *\n * @example\n * ```ts\n * bimap(parsed, (cfg) => cfg.name, (e) => ({ kind: \"input\", cause: e }));\n * ```\n */\nexport const bimap: {\n <T, U, E, F>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => F): Result<U, F>;\n <T, U, E, F>(\n onOk: (value: T) => U,\n onErr: (error: E) => F,\n ): (result: Result<T, E>) => Result<U, F>;\n} = dual(\n 3,\n <T, U, E, F>(\n result: Result<T, E>,\n onOk: (value: T) => U,\n onErr: (error: E) => F,\n ): Result<U, F> => (isOk(result) ? mkOk(onOk(result.value)) : mkErr(onErr(result.error))),\n);\n\n/**\n * Canonical bind (Fantasy Land `chain`). Chains a Result-returning step,\n * widening the error union to `E | F`. Short-circuits on `Err`.\n *\n * @example\n * ```ts\n * flatMap(parseInput(raw), (data) =>\n * data.id != null ? ok(data) : err({ kind: \"missing_id\" as const }),\n * );\n * // Result<Data, ParseError | { kind: \"missing_id\" }>\n * ```\n */\nexport const flatMap: {\n <T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F>;\n <T, U, F>(fn: (value: T) => Result<U, F>): <E>(result: Result<T, E>) => Result<U, E | F>;\n} = dual(\n 2,\n <T, U, E, F>(result: Result<T, E>, fn: (value: T) => Result<U, F>): Result<U, E | F> =>\n isOk(result) ? fn(result.value) : mkErr(result.error),\n);\n\n/**\n * Error-track bind — runs `fn` only when the result is `Err`, allowing\n * a failed workflow to recover to `Ok` or remap the failure. Mirror of\n * {@link flatMap} on the error channel.\n *\n * @example\n * ```ts\n * recover(networkResult, (e) =>\n * e.kind === \"rate_limit\" ? ok(cachedBody) : err(e),\n * );\n * ```\n */\nexport const recover: {\n <T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;\n <T, E, F>(fn: (error: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;\n} = dual(\n 2,\n <T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F> =>\n isErr(result) ? fn(result.error) : mkOk(result.value),\n);\n\n/**\n * Observe the `Ok` value for side effects (logging, metrics) without\n * changing the carried value. Passes `Err` through untouched.\n *\n * @example\n * ```ts\n * pipe(\n * parseConfig(raw),\n * tap((cfg) => log.info({ msg: \"parsed\", name: cfg.name })),\n * flatMap(validate),\n * );\n * ```\n */\nexport const tap: {\n <T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;\n <T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;\n} = dual(2, <T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E> => {\n if (isOk(result)) fn(result.value);\n return result;\n});\n\n/**\n * Observe the `Err` value for side effects (logging, metrics) without\n * changing the carried error. Passes `Ok` through untouched.\n *\n * @example\n * ```ts\n * pipe(\n * loadUser(id),\n * tapErr((e) => metrics.inc(\"user.load.fail\", { kind: e.kind })),\n * );\n * ```\n */\nexport const tapErr: {\n <T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;\n <E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;\n} = dual(2, <T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E> => {\n if (isErr(result)) fn(result.error);\n return result;\n});\n\n/**\n * Terminal collapse — fold both tracks into a single value. Dual-form:\n * 3-args data-first, 2-args curried for {@link pipe}. Returns whatever\n * the handlers return.\n *\n * For files that also import `match` from `ts-pattern`, use a namespace\n * import (`import * as R from \"@onrails/result\"` → `R.match`) to dissolve\n * the collision.\n *\n * @example\n * ```ts\n * const html = match(parsed, (cfg) => render(cfg), (e) => renderError(e));\n * ```\n */\nexport const match: {\n <T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;\n <T, E, U>(onOk: (value: T) => U, onErr: (error: E) => U): (result: Result<T, E>) => U;\n} = dual(\n 3,\n <T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U =>\n isOk(result) ? onOk(result.value) : onErr(result.error),\n);\n\n/**\n * Returns the `Ok` value, or `defaultValue` when the result is `Err`. The\n * fallback may be a different type from `T` — the return widens to `T | U`,\n * matching `ResultAsync.unwrapOr` and the compat shim.\n *\n * @example\n * ```ts\n * unwrapOr(parsedSetting, \"default-value\"); // string\n * unwrapOr(parsedPort, null); // number | null\n * ```\n */\nexport const unwrapOr: {\n <T, E, U>(result: Result<T, E>, defaultValue: U): T | U;\n <U>(defaultValue: U): <T, E>(result: Result<T, E>) => T | U;\n} = dual(2, <T, E, U>(result: Result<T, E>, defaultValue: U): T | U =>\n isOk(result) ? result.value : defaultValue,\n);\n\n/**\n * Test/assert helper — returns the `Ok` value, or **throws the original `Err`\n * value** when called on an `Err`. This is the assertion tier (RFC 0001 §4):\n * intended for `*.spec.ts` / `*.test.ts`, where throwing fails the test loudly.\n * In business logic prefer {@link match} or {@link unwrapOr}; the lint plugins\n * flag `unwrapOk` outside test files.\n *\n * @returns the unwrapped `Ok` value\n * @throws the carried `Err` value when the result is `Err`\n *\n * @example\n * ```ts\n * // in a *.spec.ts\n * expect(unwrapOk(ok(5))).toBe(5);\n * expect(() => unwrapOk(err(error))).toThrow(error);\n * ```\n */\nexport function unwrapOk<T, E>(result: Result<T, E>): T {\n if (isErr(result)) throw result.error;\n return result.value;\n}\n\n/** Alias of {@link unwrapOk} for syntax cohesion with `@onrails/maybe`. */\nexport const unwrap = unwrapOk;\n\n/**\n * Test/assert helper — returns the `Err` value, or **throws a `TypeError`**\n * when called on an `Ok`. Mirror of {@link unwrapOk} on the error track and\n * part of the same assertion tier (RFC 0001 §4): intended for `*.spec.ts` /\n * `*.test.ts`. Prefer {@link match} / {@link unwrapOr} in business logic; the\n * lint plugins flag `unwrapErr` outside test files.\n *\n * @returns the unwrapped `Err` value\n * @throws `TypeError` when the result is `Ok`\n *\n * @example\n * ```ts\n * // in a *.spec.ts\n * expect(unwrapErr(err(\"x\"))).toBe(\"x\");\n * expect(() => unwrapErr(ok(5))).toThrow(TypeError);\n * ```\n */\nexport function unwrapErr<T, E>(result: Result<T, E>): E {\n if (isOk(result)) throw new TypeError(\"unwrapErr called on Ok\");\n return result.error;\n}\n\n/**\n * Wraps a throwing sync **function**, returning a function that produces a\n * {@link Result} instead of throwing. Thrown errors pass through `onThrow` to\n * become a typed `Err`; a normal return becomes `Ok`. Same name and shape as\n * neverthrow's `Result.fromThrowable`.\n *\n * Reach for {@link trySync} instead when you want to run the call right now\n * rather than build a reusable wrapper.\n *\n * @param fn - the throwing function to wrap\n * @param onThrow - maps a thrown value to the `Err` channel\n * @returns a function with `fn`'s parameters that returns `Result<ReturnType, E>`\n *\n * @example\n * ```ts\n * type ParseError = { kind: \"parse\"; message: string };\n * const parse = fromThrowable(\n * JSON.parse,\n * (e): ParseError => ({ kind: \"parse\", message: String(e) }),\n * );\n * parse(\"{}\"); // Ok({})\n * parse(\"nope\"); // Err({ kind: \"parse\", … })\n * ```\n */\nexport function fromThrowable<A extends readonly unknown[], T, E>(\n fn: (...args: A) => T,\n onThrow: (error: unknown) => E,\n): (...args: A) => Result<T, E>;\nexport function fromThrowable<F extends (...args: never) => unknown, E>(\n fn: F,\n onThrow: (error: unknown) => E,\n): (...args: Parameters<F>) => Result<ReturnType<F>, E>;\nexport function fromThrowable(\n fn: (...args: never) => unknown,\n onThrow: (error: unknown) => unknown,\n): (...args: never) => Result<unknown, unknown> {\n return (...args: never) => {\n try {\n return mkOk(fn(...args));\n } catch (error) {\n return mkErr(onThrow(error));\n }\n };\n}\n\n/**\n * Runs `fn` **now**, catching a throw into the `Err` track — the sync mirror\n * of `tryAsync`. Without `onThrow` the thrown value is normalized to an\n * `Error`; pass a mapper for a typed error.\n *\n * To lift a throwing function once and call it many times, use\n * {@link fromThrowable}.\n *\n * @param fn - the throwing thunk to run\n * @param onThrow - maps a thrown value to the `Err` channel\n *\n * @example\n * ```ts\n * trySync(() => JSON.parse(raw)); // Result<unknown, Error>\n * trySync(() => Schema.parse(input), (e): SchemaError => ({\n * kind: \"schema\",\n * cause: e,\n * }));\n * ```\n */\nexport function trySync<T>(fn: () => T): Result<T, Error>;\nexport function trySync<T, E>(fn: () => T, onThrow: (error: unknown) => E): Result<T, E>;\nexport function trySync<T, E>(fn: () => T, onThrow?: (error: unknown) => E): Result<T, E | Error> {\n try {\n return mkOk(fn());\n } catch (error) {\n return mkErr(onThrow ? onThrow(error) : toError(error));\n }\n}\n\nconst printPayload = (payload: unknown): string => {\n try {\n return JSON.stringify(payload) ?? String(payload);\n } catch {\n // cyclic or non-JSON payload — a debug printer must not throw\n return String(payload);\n }\n};\n\n/**\n * Debug printer — renders a result as `Ok(…)` / `Err(…)` for logs. Payloads\n * print as JSON (values are plain data by design); non-JSON payloads fall\n * back to `String(...)`.\n *\n * @example\n * ```ts\n * show(ok(1)); // 'Ok(1)'\n * show(err({ kind: \"e\" })); // 'Err({\"kind\":\"e\"})'\n * ```\n */\nexport const show = <T, E>(result: Result<T, E>): string =>\n isOk(result) ? `Ok(${printPayload(result.value)})` : `Err(${printPayload(result.error)})`;\n","import { mkErr, mkOk } from \"./internal/ctor.js\";\n\n/**\n * Plain-data shape of a {@link Result}: what `JSON.parse`, `structuredClone`\n * and `postMessage` hand back. It has no methods; lift it with `Result.from`.\n */\nexport type ResultData<T, E> =\n | { readonly _tag: \"Ok\"; readonly value: T }\n | { readonly _tag: \"Err\"; readonly error: E };\n\n/**\n * Methods every live {@link Result} carries on its (non-enumerable) prototype.\n * Core semantics: `tap` does not swallow throws, `recover` keeps the `Ok` type.\n * Method syntax keeps the type parameters bivariant, so `Result<T, never>` is\n * assignable to `Result<T, E>`.\n */\nexport interface ResultMethods<T, E> {\n /** Transform the `Ok` value; `Err` passes through. */\n map<U>(fn: (value: T) => U): Result<U, E>;\n /** Transform the `Err` value; `Ok` passes through. */\n mapErr<F>(fn: (error: E) => F): Result<T, F>;\n /** Canonical bind; widens the error union and short-circuits on `Err`. */\n flatMap<U, F>(fn: (value: T) => Result<U, F>): Result<U, E | F>;\n /** Alias of {@link ResultMethods.flatMap} for neverthrow muscle memory. */\n andThen<U, F>(fn: (value: T) => Result<U, F>): Result<U, E | F>;\n /** Error-track bind; recover to `Ok` or remap the failure. */\n recover<F>(fn: (error: E) => Result<T, F>): Result<T, F>;\n /** Observe the `Ok` value, pass the result through. */\n tap(fn: (value: T) => void): Result<T, E>;\n /** Observe the `Err` value, pass the result through. */\n tapErr(fn: (error: E) => void): Result<T, E>;\n /** Fold both tracks into one value. */\n match<U>(onOk: (value: T) => U, onErr: (error: E) => U): U;\n /** The `Ok` value, or `defaultValue` on `Err`. */\n unwrapOr<U>(defaultValue: U): T | U;\n}\n\n/**\n * The core railway type: a tagged union carrying either a success value\n * (`Ok<T>`) or a failure value (`Err<E>`), discriminated on `_tag`. Carriers\n * are plain data (`_tag` plus `value` / `error` as own enumerable properties)\n * with {@link ResultMethods} on a shared non-enumerable prototype, so\n * `JSON.stringify` output is unchanged. Narrow on `_tag` to read\n * `.value` / `.error`.\n *\n * Build carriers with `ok` / `err`; a hand-written `{ _tag: \"Ok\", value }`\n * literal has no methods and is not a `Result`. Data that crossed a\n * serialization boundary is lifted back with `Result.from`.\n *\n * @typeParam T - the `Ok` value type\n * @typeParam E - the `Err` error type\n *\n * @example\n * ```ts\n * function parse(raw: string): Result<number, \"nan\"> {\n * const n = Number(raw);\n * return Number.isNaN(n) ? err(\"nan\") : ok(n);\n * }\n * parse(\"1\").map((n) => n + 1);\n * ```\n */\nexport type Result<T, E> = ResultData<T, E> & ResultMethods<T, E>;\n\n/**\n * Value-side companion of the {@link Result} type.\n *\n * `from` lifts plain data back into a live `Result`. Use it at every\n * deserialization sink (`JSON.parse`, `structuredClone`, `postMessage`,\n * IndexedDB, Redux rehydration), where the prototype and its methods are lost\n * while the type still claims them. It trusts the shape; validate untrusted\n * input first. An unknown `_tag` is a defect and throws a `TypeError` rather\n * than becoming a silent `Err(undefined)`.\n *\n * @example\n * ```ts\n * const wire = JSON.parse(JSON.stringify(ok(1))); // plain data, no methods\n * const live = Result.from<number, string>(wire); // Result<number, string>\n * live.map((n) => n + 1);\n * ```\n */\nexport const Result = {\n from: <T, E>(data: ResultData<T, E>): Result<T, E> => {\n if (data._tag === \"Ok\") return mkOk<T, E>(data.value);\n if (data._tag === \"Err\") return mkErr<T, E>(data.error);\n throw new TypeError(`Result.from: unknown _tag ${String((data as { _tag?: unknown })._tag)}`);\n },\n} as const;\n\n/**\n * The success branch of a {@link Result} — `{ _tag: \"Ok\"; value: T }`.\n * The `E` parameter keeps the type assignable to `Result<T, E>`.\n */\nexport type Ok<T, E = never> = Extract<Result<T, E>, { _tag: \"Ok\" }>;\n\n/**\n * The failure branch of a {@link Result} — `{ _tag: \"Err\"; error: E }`.\n */\nexport type Err<T, E> = Extract<Result<T, E>, { _tag: \"Err\" }>;\n\n/**\n * Error subclass representing an **unexpected** defect — a thrown exception or\n * promise rejection that was not part of the modelled `Err` union. Used as the\n * default mapping when {@link fromAsync} is called without an `onDefect`\n * handler; the original cause is preserved on `.cause`.\n *\n * @example\n * ```ts\n * const defect = new UnexpectedError(\"Unexpected async defect\", caught);\n * defect.cause; // the original thrown value\n * ```\n */\nexport class UnexpectedError extends Error {\n constructor(\n message: string,\n override readonly cause?: unknown,\n ) {\n super(message);\n this.name = \"UnexpectedError\";\n }\n}\n","import { mkErr, mkOk } from \"./internal/ctor.js\";\nimport type { InferErr, InferOk } from \"./internal/infer.js\";\nimport type { Payload } from \"./internal/payload.js\";\nimport { bimap, isErr, map, mapErr } from \"./result.js\";\nimport type { Result } from \"./types.js\";\nimport { UnexpectedError } from \"./types.js\";\n\ntype PromiseFactory<T, E> = () => Promise<Result<T, E>>;\ntype AnyResultAsync = ResultAsync<unknown, unknown>;\n\n/**\n * Mirrors the sync `CombineTuple` in `./collections.ts`: the element check\n * lives in the RETURN type, never in the parameter constraint. A\n * `ResultAsync<unknown, unknown>` constraint contextually types the arguments\n * and widens `okAsync(1)`'s defaulted `E = never` to `unknown`, so call sites\n * would need hoisted intermediates to keep their error union.\n */\ntype CombineTupleAsync<R extends readonly unknown[]> = R[number] extends AnyResultAsync\n ? ResultAsync<{ [K in keyof R]: InferOk<R[K]> }, InferErr<R[number]>>\n : never;\n\n/** Mirrors `TupleGuard` in `./collections.ts` — see the note there. */\ntype AsyncTupleGuard<R extends readonly unknown[]> = R[number] extends AnyResultAsync\n ? []\n : [error: \"expected an array of ResultAsync values\"];\n\n/** Collapses settled results left-to-right, short-circuiting on the first `Err`. */\nconst sequenceSettled = (\n settled: readonly Result<unknown, unknown>[],\n): Result<unknown[], unknown> => {\n const values: unknown[] = [];\n for (const result of settled) {\n if (isErr(result)) {\n return mkErr(result.error);\n }\n values.push(result.value);\n }\n return mkOk(values);\n};\n\n/**\n * Async railway carrier. Wraps a deferred `Promise<Result<T, E>>` and exposes\n * the same dual-track transforms as the sync {@link Result} — `map`, `mapErr`,\n * `bimap`, `flatMap`, `recover`, `tap`, `match`. The public API never surfaces `Promise<Result<…>>`\n * directly: `await` the instance (it is thenable) or call {@link resolve} /\n * {@link match} to settle it.\n *\n * Construct via the static factories ({@link ok}, {@link err}, {@link fromPromise},\n * {@link defer}, …) rather than `new` — the constructor is `protected`.\n *\n * @typeParam T - the `Ok` value type\n * @typeParam E - the `Err` error type\n *\n * @example\n * ```ts\n * const user = ResultAsync.fromPromise(api.getUser(id), toAppError)\n * .map((u) => u.profile)\n * .recover(() => ResultAsync.ok(guestProfile));\n *\n * const r = await user; // Result<Profile, AppError>\n * ```\n */\nexport class ResultAsync<T, E> {\n private promise: Promise<Result<T, E>> | null = null;\n\n protected constructor(protected readonly run: PromiseFactory<T, E>) {}\n\n /**\n * Lifts an already-settled sync {@link Result} into a {@link ResultAsync}.\n *\n * @example\n * ```ts\n * const ra = ResultAsync.fromResult(ok(42)); // ResultAsync<number, never>\n * ```\n */\n static fromResult<T, E>(result: Result<T, E>): ResultAsync<T, E> {\n return new ResultAsync(async () => result);\n }\n\n /**\n * Wraps a `PromiseLike<T>` that may reject. Rejections pass through `onReject`\n * to become a typed `Err`; resolution becomes `Ok<T>`.\n *\n * @param promise - the promise to wrap\n * @param onReject - maps a rejection reason to the `Err` channel\n *\n * @example\n * ```ts\n * const body = ResultAsync.fromPromise(\n * fetch(url).then((r) => r.text()),\n * (e): NetError => ({ kind: \"net\", cause: String(e) }),\n * );\n * ```\n */\n static fromPromise<T, E>(\n promise: PromiseLike<T>,\n onReject: (error: unknown) => E,\n ): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n try {\n return mkOk(await promise);\n } catch (error) {\n return mkErr(onReject(error));\n }\n });\n }\n\n /**\n * Wraps a `PromiseLike<T>` that is **guaranteed not to reject**, skipping the\n * `onReject` mapper. Use only when rejection is provably impossible.\n *\n * @example\n * ```ts\n * const now = ResultAsync.fromSafePromise(Promise.resolve(Date.now()));\n * ```\n */\n static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E> {\n return new ResultAsync(async () => mkOk(await promise));\n }\n\n /**\n * Defers work until {@link resolve}. Unlike {@link fromPromise}, the factory\n * does not run until the `ResultAsync` is resolved (e.g. by `combineTuple` /\n * `combineTupleParallel`). The factory runs at most once — `resolve` memoizes.\n *\n * @example\n * ```ts\n * const insert = ResultAsync.defer(() => db.orders.insert(row));\n * // nothing has run yet\n * const r = await insert; // factory runs exactly once here\n * ```\n */\n static defer<T, E>(fn: () => Promise<Result<T, E>>): ResultAsync<T, E> {\n return new ResultAsync(fn);\n }\n\n /**\n * Lifts a value into an `Ok` async result.\n *\n * @example\n * ```ts\n * const r = ResultAsync.ok(42); // ResultAsync<number, never>\n * ```\n */\n static ok<const T extends Payload[]>(value: T): ResultAsync<T, never>;\n static ok<const T>(value: T): ResultAsync<T, never>;\n static ok<T, E>(value: T): ResultAsync<T, E>;\n static ok<T, E = never>(value: T): ResultAsync<T, E> {\n return new ResultAsync(async () => mkOk(value));\n }\n\n /**\n * Fantasy Land `pure` — alias of {@link ResultAsync.ok}. One lift name\n * shared across the trio (`of` / `Maybe.of` / `ResultAsync.of`).\n */\n static of<const T extends Payload[]>(value: T): ResultAsync<T, never>;\n static of<const T>(value: T): ResultAsync<T, never>;\n static of<T, E>(value: T): ResultAsync<T, E>;\n static of<T, E = never>(value: T): ResultAsync<T, E> {\n return ResultAsync.ok<T, E>(value);\n }\n\n /**\n * Lifts an error into an `Err` async result.\n *\n * @example\n * ```ts\n * const r = ResultAsync.err({ kind: \"not_found\" as const });\n * ```\n */\n static err<const E extends Payload[]>(error: E): ResultAsync<never, E>;\n static err<const E>(error: E): ResultAsync<never, E>;\n static err<T, E>(error: E): ResultAsync<T, E>;\n static err<T = never, E = unknown>(error: E): ResultAsync<T, E> {\n return new ResultAsync(async () => mkErr(error));\n }\n\n /**\n * Lifts an existing `Promise<Result<T, E>>` (e.g. from interop code) into a\n * {@link ResultAsync}. A thrown defect is routed through `onDefect`,\n * defaulting to {@link UnexpectedError}. The `Ok` value passes through\n * verbatim — even when it happens to be Result-shaped.\n *\n * @param promise - a promise that already yields a `Result`\n * @param onDefect - maps an unexpected throw to the `Err` channel\n * @see {@link fromAsync} from `@onrails/result`\n */\n static fromResultPromise<T, E>(\n promise: Promise<Result<T, E>>,\n onDefect?: (error: unknown) => E | UnexpectedError,\n ): ResultAsync<T, E | UnexpectedError> {\n const mapDefect =\n onDefect ?? ((error: unknown) => new UnexpectedError(\"Unexpected async defect\", error));\n return new ResultAsync<T, E | UnexpectedError>(async () => {\n try {\n return await promise;\n } catch (error) {\n return mkErr(mapDefect(error));\n }\n });\n }\n\n /**\n * Combines a homogeneous array of async results into one. Resolves\n * **sequentially** in input order, short-circuiting on the first `Err`.\n * For heterogeneous tuples that preserve per-index types, use\n * {@link combineTuple}; for wall-clock overlap, {@link combineTupleParallel}.\n *\n * @example\n * ```ts\n * const all = ResultAsync.combine([loadA(), loadB(), loadC()]);\n * // ResultAsync<Item[], LoadError>\n * ```\n */\n static combine<T, E>(results: readonly ResultAsync<T, E>[]): ResultAsync<T[], E> {\n return new ResultAsync(async () => {\n const values: T[] = [];\n for (const ra of results) {\n const result = await ra.resolve();\n if (isErr(result)) {\n return mkErr(result.error);\n }\n values.push(result.value);\n }\n return mkOk(values);\n });\n }\n\n /**\n * Heterogeneous async tuple combine — resolves branches **sequentially**\n * (left-to-right), returning the first `Err` in input order. Preserves each\n * branch's `Ok` type by position, so the result destructures type-safely.\n * This is the canonical sequential async combine (replaces the former\n * `sequenceTupleAsync`).\n *\n * @example\n * ```ts\n * const combined = ResultAsync.combineTuple([loadCfg(), loadCatalog()]);\n * // ResultAsync<readonly [Cfg, Catalog], CfgError | CatalogError>\n * const r = await combined;\n * if (isOk(r)) {\n * const [cfg, catalog] = r.value; // typed per position\n * }\n * ```\n */\n static combineTuple<const R extends readonly unknown[]>(\n results: R,\n ..._guard: AsyncTupleGuard<R>\n ): CombineTupleAsync<R> {\n // Runtime identical to combine; the cast restores per-index tuple types.\n return ResultAsync.combine(\n results as readonly AnyResultAsync[],\n ) as unknown as CombineTupleAsync<R>;\n }\n\n /**\n * Like {@link combineTuple}, but starts every branch before awaiting (wall-clock\n * parallel for independent IO). On failure, returns the first `Err` in input order.\n *\n * @example\n * ```ts\n * // independent IO — overlap them\n * const combined = ResultAsync.combineTupleParallel([\n * loadProfile(id),\n * loadMetrics(id),\n * ]);\n * ```\n */\n static combineTupleParallel<const R extends readonly unknown[]>(\n results: R,\n ..._guard: AsyncTupleGuard<R>\n ): CombineTupleAsync<R> {\n // Cast restores per-index tuple types over the untyped sequence core.\n return new ResultAsync(async () =>\n sequenceSettled(\n await Promise.all((results as readonly AnyResultAsync[]).map((ra) => ra.resolve())),\n ),\n ) as unknown as CombineTupleAsync<R>;\n }\n\n /**\n * Transforms the `Ok` value, passing `Err` through unchanged.\n *\n * @example\n * ```ts\n * ResultAsync.ok(2).map((n) => n * 3); // ResultAsync<number> → Ok 6\n * ```\n */\n map<U>(fn: (value: T) => U): ResultAsync<U, E> {\n return new ResultAsync(async () => map(await this.resolve(), fn));\n }\n\n /**\n * Transforms the `Err` value, passing `Ok` through unchanged — useful for\n * unifying heterogeneous failures into one app-level union.\n *\n * @example\n * ```ts\n * load(id).mapErr((e): AppError => ({ kind: \"load\", cause: e })); // ResultAsync<T, AppError>\n * ```\n */\n mapErr<F>(fn: (error: E) => F): ResultAsync<T, F> {\n return new ResultAsync(async () => mapErr(await this.resolve(), fn));\n }\n\n /**\n * Transforms both tracks in one step — the async mirror of the sync\n * `bimap`. Exactly one of `onOk` / `onErr` runs.\n *\n * @example\n * ```ts\n * load(id).bimap(toDto, (e): AppError => ({ kind: \"load\", cause: e }));\n * ```\n */\n bimap<U, F>(onOk: (value: T) => U, onErr: (error: E) => F): ResultAsync<U, F> {\n return new ResultAsync(async () => bimap(await this.resolve(), onOk, onErr));\n }\n\n /**\n * Canonical bind — chains a step that itself returns a `ResultAsync` or sync\n * `Result`, short-circuiting on `Err`. Error types accumulate (`E | F`).\n *\n * @example\n * ```ts\n * authenticate(req)\n * .flatMap((user) => loadProfile(user.id)) // ResultAsync\n * .flatMap((p) => validate(p)); // sync Result also accepted\n * ```\n */\n flatMap<U, F = E>(fn: (value: T) => ResultAsync<U, F> | Result<U, F>): ResultAsync<U, E | F> {\n return new ResultAsync<U, E | F>(async () => {\n const first = await this.resolve();\n if (isErr(first)) {\n return mkErr(first.error);\n }\n const next = fn(first.value);\n return next instanceof ResultAsync ? next.resolve() : next;\n });\n }\n\n /**\n * neverthrow-compat alias of {@link flatMap}. Kept as the documented compat\n * tier; prefer {@link flatMap} in new code.\n *\n * @example\n * ```ts\n * authenticate(req).andThen((user) => loadProfile(user.id));\n * ```\n */\n andThen<U, F = E>(fn: (value: T) => ResultAsync<U, F> | Result<U, F>): ResultAsync<U, E | F> {\n return this.flatMap(fn);\n }\n\n /**\n * Error-channel bind — runs `fn` only on `Err`, swapping in a recovery\n * `ResultAsync` or sync `Result`. `Ok` passes through. The mirror of\n * {@link flatMap} on the error track.\n *\n * @example\n * ```ts\n * loadFromCache(id).recover(() => loadFromOrigin(id)); // ResultAsync<T, F>\n * ```\n */\n recover<F>(fn: (error: E) => ResultAsync<T, F> | Result<T, F>): ResultAsync<T, F> {\n return new ResultAsync<T, F>(async () => {\n const first = await this.resolve();\n if (!isErr(first)) {\n return mkOk(first.value);\n }\n const next = fn(first.error);\n return next instanceof ResultAsync ? next.resolve() : next;\n });\n }\n\n /**\n * neverthrow-compat alias of {@link recover}. Kept as the documented compat\n * tier; prefer {@link recover} in new code.\n *\n * @example\n * ```ts\n * loadFromCache(id).orElse(() => loadFromOrigin(id));\n * ```\n */\n orElse<F>(fn: (error: E) => ResultAsync<T, F> | Result<T, F>): ResultAsync<T, F> {\n return this.recover(fn);\n }\n\n /**\n * Runs a side effect on the `Ok` value and passes the result through\n * unchanged; a no-op on `Err`. Mirrors {@link tapErr}.\n *\n * @example\n * ```ts\n * saveUser(u).tap((saved) => analytics.track(\"user_saved\", saved.id));\n * ```\n */\n tap(fn: (value: T) => void): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n const result = await this.resolve();\n if (!isErr(result)) {\n fn(result.value);\n }\n return result;\n });\n }\n\n /**\n * Runs a side effect on the `Err` value and passes the result through\n * unchanged; a no-op on `Ok`. The error-track mirror of {@link tap}.\n *\n * @example\n * ```ts\n * saveUser(u).tapErr((e) => logger.warn(\"save failed\", e));\n * ```\n */\n tapErr(fn: (error: E) => void): ResultAsync<T, E> {\n return new ResultAsync(async () => {\n const result = await this.resolve();\n if (isErr(result)) {\n fn(result.error);\n }\n return result;\n });\n }\n\n /**\n * Settles to the `Ok` value, or `defaultValue` if `Err`. Terminal — returns a\n * plain `Promise`, not a `ResultAsync`.\n *\n * @example\n * ```ts\n * const profile = await loadProfile(id).unwrapOr(guestProfile);\n * ```\n */\n unwrapOr<U>(defaultValue: U): Promise<T | U> {\n return this.resolve().then((result) => (isErr(result) ? defaultValue : result.value));\n }\n\n /**\n * Terminal collapse — folds both tracks into a single awaited value. Returns\n * a plain `Promise`, settling the carrier exactly once.\n *\n * @param onOk - handles the `Ok` value\n * @param onErr - handles the `Err` value\n *\n * @example\n * ```ts\n * const status = await save(row).match(\n * () => 200,\n * (e) => (e.kind === \"conflict\" ? 409 : 500),\n * );\n * ```\n */\n match<U1, U2 = U1>(onOk: (value: T) => U1, onErr: (error: E) => U2): Promise<U1 | U2> {\n return this.resolve().then((result) =>\n isErr(result) ? onErr(result.error) : onOk(result.value),\n );\n }\n\n /**\n * Settles the carrier to a bare sync {@link Result}, memoizing so the\n * underlying factory runs at most once. Prefer `await ra` (the thenable) or\n * {@link match} in app code; use `resolve` when you need the tagged union back.\n *\n * @example\n * ```ts\n * const r = await load(id).resolve(); // Result<Data, LoadError>\n * if (isOk(r)) use(r.value);\n * ```\n */\n resolve(): Promise<Result<T, E>> {\n if (!this.promise) {\n this.promise = this.run();\n }\n return this.promise;\n }\n\n /**\n * Thenable shim — `await ra` resolves to a bare tagged-union `Result<T, E>`.\n * Narrow with `isOk(r)` / `isErr(r)` to read `.value` / `.error`.\n */\n // biome-ignore lint/suspicious/noThenProperty: makes ResultAsync awaitable\n then<R1 = Result<T, E>, R2 = never>(\n onfulfilled?: ((value: Result<T, E>) => R1 | PromiseLike<R1>) | undefined | null,\n onrejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | undefined | null,\n ): Promise<R1 | R2> {\n return this.resolve().then(\n // Safe: without onfulfilled, R1 stays at its Result<T, E> default.\n (r) => (onfulfilled ? onfulfilled(r) : (r as unknown as R1)),\n onrejected ?? undefined,\n );\n }\n}\n\n/**\n * The free-function lift helpers (`okAsync`, `errAsync`, `fromPromise`,\n * `fromSafePromise`, `fromResult`, `fromAsync`, `asyncAfter`, `tryAsync`)\n * live in `./async-lift.ts` and are re-exported from the package index.\n */\n","import { ResultAsync } from \"./async.js\";\nimport { mkErr, mkOk } from \"./internal/ctor.js\";\nimport { flatMap, fromThrowable, map } from \"./result.js\";\nimport type { Result } from \"./types.js\";\n\n/**\n * Tracks whether a {@link Railway} workflow has crossed an async boundary.\n * Sync workflows return {@link Result}; async workflows return {@link ResultAsync}.\n */\nexport type RailwayMode = \"sync\" | \"async\";\n\n/**\n * Mode-aware output type for a {@link Railway} workflow: sync mode →\n * `Result<T, E>`, async mode → `ResultAsync<T, E>`.\n */\nexport type RailwayOutput<T, E, M extends RailwayMode> = M extends \"async\"\n ? ResultAsync<T, E>\n : Result<T, E>;\n\n/**\n * Runtime carrier for a workflow. Invariant: the `mode` tag mirrors the\n * phantom `M` on {@link Railway} — every construction site writes matching\n * tag and type parameter, and {@link Railway.step} / {@link Railway.out}\n * are the only places that re-link the two.\n */\ntype RailwayState<C extends object, E> =\n | { readonly mode: \"sync\"; readonly result: Result<C, E> }\n | { readonly mode: \"async\"; readonly result: ResultAsync<C, E> };\n\nexport type BranchFn<C extends object> = (ctx: C) => ResultAsync<unknown, unknown>;\nexport type BranchRecord = Record<string, (ctx: never) => ResultAsync<unknown, unknown>>;\ntype BranchOk<R> = R extends (ctx: never) => ResultAsync<infer T, infer _E> ? T : never;\ntype BranchErr<R> = R extends (ctx: never) => ResultAsync<infer _T, infer E> ? E : never;\nexport type ParallelOutput<R extends BranchRecord> = {\n [K in keyof R]: BranchOk<R[K]>;\n};\nexport type ParallelError<R extends BranchRecord> = BranchErr<R[keyof R]>;\n\n/** Collapses an intersection into a single displayed object type. */\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\n/**\n * The context after one more named step. Chaining `C & Record<K, T>` instead\n * builds an intersection that TS echoes verbatim in every hover and error\n * message (`Record<never, never> & Record<\"id\", string> & …`); `Omit` keeps\n * property modifiers and `Prettify` collapses the result to a flat object.\n */\ntype Extend<C extends object, K extends string, T> = Prettify<Omit<C, K> & Record<K, T>>;\n\n/**\n * Rejects a key already present in the context. Railway merges each step with\n * a spread, so a repeated key overwrites at runtime while the old\n * `C & Record<K, T>` type intersected the two field types — `number & string`\n * became `never` and nothing complained. Resolves to `[]` for a fresh key and\n * to an unsatisfiable extra parameter otherwise, so the collision fails at the\n * call site with the offending name in the message.\n */\ntype FreshKey<C, K extends string> = K extends keyof C\n ? [error: `key \"${K}\" already exists in the workflow context`]\n : [];\n\n/** {@link FreshKey} for a whole record of parallel branches. */\ntype FreshKeys<C, R> =\n Extract<keyof R, keyof C> extends never\n ? []\n : [\n error: `branch key \"${Extract<keyof R, keyof C> & string}\" already exists in the workflow context`,\n ];\n\nconst addField = <C extends object, K extends string, T>(\n ctx: C,\n key: K,\n value: T,\n): Extend<C, K, T> => ({ ...ctx, [key]: value }) as Extend<C, K, T>;\n\n/**\n * Named-context workflow builder. Each step appends a typed field to the\n * accumulating context object; the workflow tracks sync/async mode so the\n * final output type ({@link RailwayOutput}) is correct.\n */\nexport class Railway<C extends object, E, M extends RailwayMode> {\n private constructor(private readonly state: RailwayState<C, E>) {}\n\n /** Start an empty sync workflow with no fields in context. */\n static empty(): Railway<Record<never, never>, never, \"sync\"> {\n return new Railway({ mode: \"sync\", result: mkOk({}) });\n }\n\n /** Start a sync workflow with the given context as the initial state. */\n static context<C extends object>(context: C): Railway<C, never, \"sync\"> {\n return new Railway({ mode: \"sync\", result: mkOk(context) });\n }\n\n /** Start a sync workflow with a throwing function. */\n static fromSync<K extends string, T, E>(\n key: K,\n fn: () => T,\n onThrow: (error: unknown) => E,\n ): Railway<Record<K, T>, E, \"sync\"> {\n return Railway.empty().addSync(key, () => fromThrowable(fn, onThrow)()) as Railway<\n Record<K, T>,\n E,\n \"sync\"\n >;\n }\n\n /** Start a sync workflow with a `Result`-returning function. */\n static fromResult<K extends string, T, E>(\n key: K,\n fn: () => Result<T, E>,\n ): Railway<Record<K, T>, E, \"sync\"> {\n return Railway.empty().addSync(key, fn) as Railway<Record<K, T>, E, \"sync\">;\n }\n\n /** Start an async workflow with a `PromiseLike`-returning function. */\n static fromPromise<K extends string, T, E>(\n key: K,\n fn: () => PromiseLike<T>,\n onReject: (error: unknown) => E,\n ): Railway<Record<K, T>, E, \"async\"> {\n return Railway.empty().addAsync(key, () => ResultAsync.fromPromise(fn(), onReject)) as Railway<\n Record<K, T>,\n E,\n \"async\"\n >;\n }\n\n /** Start an async workflow with a `ResultAsync`-returning function. */\n static fromAsync<K extends string, T, E>(\n key: K,\n fn: () => ResultAsync<T, E>,\n ): Railway<Record<K, T>, E, \"async\"> {\n return Railway.empty().addAsync(key, fn) as Railway<Record<K, T>, E, \"async\">;\n }\n\n /**\n * Rebuild the workflow in its current mode after transforming the carried\n * result. Single re-link point for the state/phantom invariant: the mode\n * tag is preserved verbatim, so `M` still describes the new state — TS\n * cannot narrow the phantom `M` from the runtime tag, hence the cast.\n */\n private step<C2 extends object, F>(\n onSync: (result: Result<C, E>) => Result<C2, E | F>,\n onAsync: (result: ResultAsync<C, E>) => ResultAsync<C2, E | F>,\n ): Railway<C2, E | F, M> {\n const next: RailwayState<C2, E | F> =\n this.state.mode === \"sync\"\n ? { mode: \"sync\", result: onSync(this.state.result) }\n : { mode: \"async\", result: onAsync(this.state.result) };\n return new Railway(next) as Railway<C2, E | F, M>;\n }\n\n /**\n * Project the carried result into the mode-aware output type. Counterpart\n * of {@link Railway.step} for terminal steps: the runtime branch matches\n * the branch `RailwayOutput` picks for `M` (state/phantom invariant), but\n * TS cannot resolve the conditional on an unresolved `M`, hence the cast.\n */\n private out<T>(\n onSync: (result: Result<C, E>) => Result<T, E>,\n onAsync: (result: ResultAsync<C, E>) => ResultAsync<T, E>,\n ): RailwayOutput<T, E, M> {\n return (\n this.state.mode === \"sync\" ? onSync(this.state.result) : onAsync(this.state.result)\n ) as RailwayOutput<T, E, M>;\n }\n\n /** Lift the carried result into `ResultAsync` for async-upgrading steps. */\n private toAsync(): ResultAsync<C, E> {\n return this.state.mode === \"sync\"\n ? ResultAsync.fromResult(this.state.result)\n : this.state.result;\n }\n\n /**\n * Internal plumbing for a sync field append. Public steps wrap this and add\n * the {@link FreshKey} collision guard; the static factories bypass the\n * guard because they always start from an empty context.\n */\n private addSync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => Result<T, F>,\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.step<Extend<C, K, T>, F>(\n (result) => flatMap(result, (ctx) => map(fn(ctx), (value) => addField(ctx, key, value))),\n (result) =>\n result.flatMap((ctx) =>\n ResultAsync.fromResult(fn(ctx)).map((value) => addField(ctx, key, value)),\n ),\n );\n }\n\n /** Internal plumbing for an async field append — upgrades the mode. */\n private addAsync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => ResultAsync<T, F>,\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return new Railway({\n mode: \"async\",\n result: this.toAsync().flatMap((ctx) => fn(ctx).map((value) => addField(ctx, key, value))),\n });\n }\n\n /** Pure sync derivation. */\n derive<K extends string, T>(\n key: K,\n fn: (ctx: C) => T,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E, M> {\n return this.addSync(key, (ctx) => mkOk(fn(ctx)));\n }\n\n /** Throwing sync transform. */\n fromSync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => T,\n onThrow: (error: unknown) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.addSync(key, (ctx) => fromThrowable(fn, onThrow)(ctx));\n }\n\n /** Sync `Result`-returning step. */\n fromResult<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => Result<T, F>,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, M> {\n return this.addSync(key, fn);\n }\n\n /** Promise-returning step — upgrades the workflow to async mode. */\n fromPromise<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => PromiseLike<T>,\n onReject: (error: unknown) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return this.addAsync(key, (ctx) => ResultAsync.fromPromise(fn(ctx), onReject));\n }\n\n /** `ResultAsync`-returning step — upgrades the workflow to async mode. */\n fromAsync<K extends string, T, F>(\n key: K,\n fn: (ctx: C) => ResultAsync<T, F>,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, T>, E | F, \"async\"> {\n return this.addAsync(key, fn);\n }\n\n /** Narrow a nullable context field into a required non-null field. */\n require<K extends string, S extends keyof C, F>(\n key: K,\n source: S,\n onMissing: (ctx: C) => F,\n ..._guard: FreshKey<C, K>\n ): Railway<Extend<C, K, NonNullable<C[S]>>, E | F, M> {\n return this.addSync(key, (ctx) => {\n const value = ctx[source];\n return value == null ? mkErr(onMissing(ctx)) : mkOk(value as NonNullable<C[S]>);\n });\n }\n\n /** Run independent `ResultAsync` branches concurrently and merge outputs. */\n parallel<R extends Record<string, BranchFn<C>>>(\n branches: R,\n ..._guard: FreshKeys<C, R>\n ): Railway<Prettify<Omit<C, keyof R> & ParallelOutput<R>>, E | ParallelError<R>, \"async\"> {\n const merged = this.toAsync().flatMap((ctx) =>\n ResultAsync.combineTupleParallel(\n Object.entries(branches).map(([, branch]) => branch(ctx)),\n ).map(\n (values) =>\n // Branch outputs land under exactly R's keys; Object.fromEntries\n // erases that correspondence, so reassert the merged shape.\n ({\n ...ctx,\n ...Object.fromEntries(Object.keys(branches).map((key, index) => [key, values[index]])),\n }) as Prettify<Omit<C, keyof R> & ParallelOutput<R>>,\n ),\n );\n return new Railway({\n mode: \"async\",\n // BranchFn erases per-key types to `ResultAsync<unknown, unknown>`\n // (ParallelOutput/ParallelError recover them from R), so re-assert the\n // precise value/error union the branches actually produce.\n result: merged as ResultAsync<\n Prettify<Omit<C, keyof R> & ParallelOutput<R>>,\n E | ParallelError<R>\n >,\n });\n }\n\n /** Project the final context into the workflow's output type. */\n select<T>(fn: (ctx: C) => T): RailwayOutput<T, E, M> {\n return this.out(\n (result) => map(result, fn),\n (result) => result.map(fn),\n );\n }\n\n /** Return the accumulated context as-is. */\n done(): RailwayOutput<C, E, M> {\n return this.out(\n (result) => result,\n (result) => result,\n );\n }\n}\n"]}
package/dist/try-gen.cjs CHANGED
@@ -1,10 +1,47 @@
1
1
  'use strict';
2
2
 
3
- // src/internal/ctor.ts
4
- var mkErr = (error) => ({
5
- _tag: "Err",
6
- error
7
- });
3
+ // src/internal/proto.ts
4
+ var INSPECT = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
5
+ var ErrImpl = class _ErrImpl {
6
+ _tag;
7
+ error;
8
+ constructor(error) {
9
+ this._tag = "Err";
10
+ this.error = error;
11
+ }
12
+ [INSPECT](_depth, options, inspect) {
13
+ return `Err(${inspect(this.error, options)})`;
14
+ }
15
+ map(_fn) {
16
+ return new _ErrImpl(this.error);
17
+ }
18
+ mapErr(fn) {
19
+ return new _ErrImpl(fn(this.error));
20
+ }
21
+ flatMap(_fn) {
22
+ return new _ErrImpl(this.error);
23
+ }
24
+ andThen(_fn) {
25
+ return new _ErrImpl(this.error);
26
+ }
27
+ recover(fn) {
28
+ return fn(this.error);
29
+ }
30
+ tap(_fn) {
31
+ return this;
32
+ }
33
+ tapErr(fn) {
34
+ fn(this.error);
35
+ return this;
36
+ }
37
+ match(_onOk, onErr) {
38
+ return onErr(this.error);
39
+ }
40
+ unwrapOr(defaultValue) {
41
+ return defaultValue;
42
+ }
43
+ };
44
+ var newErr = (error) => new ErrImpl(error);
8
45
  var isErr = (result) => result._tag === "Err";
9
46
 
10
47
  // src/try-gen.ts
@@ -27,7 +64,7 @@ var tryGen = (fn) => {
27
64
  return fn();
28
65
  } catch (error) {
29
66
  if (error instanceof ErrSignal) {
30
- return mkErr(error.error);
67
+ return newErr(error.error);
31
68
  }
32
69
  throw error;
33
70
  }