types
@sandlada/result (root barrel) and @sandlada/result/types — the discriminated-union contracts every other module builds on.
- Operates on: the value shapes themselves —
IResult,IResultOfT,IOption,AsyncResult,AsyncOption. - Execution model: none. Every named export is a type; the only runtime export is an empty
defaultobject that materializes the entries and their sourcemaps. - Not here: constructors (
factories), transformations (operators,option), async carriers (async-result,promise-result).
Result
Section titled “Result”| Export | Description | Source |
|---|---|---|
IResult (type) |
Void result union: IResultSuccess | IResultFailure<TError>; TError defaults to unknown. |
IResult.ts |
IResultSuccess (type) |
Success variant without a value (isSuccess: true, isFailure: false). |
IResult.ts |
IResultFailure (type) |
Failure variant carrying error: TError (isSuccess: false, isFailure: true). |
IResult.ts |
IResultOfT (type) |
Value-bearing union: IResultOfTSuccess<TValue> | IResultOfTFailure<TError>; TError defaults to unknown. |
IResultOfT.ts |
IResultOfTSuccess (type) |
Success variant carrying value: TValue (isFailure: false). |
IResultOfT.ts |
IResultOfTFailure (type) |
Failure variant carrying error: TError (isSuccess: false). |
IResultOfT.ts |
Option
Section titled “Option”| Export | Description | Source |
|---|---|---|
IOption (type) |
Optional-value union: IOptionSome<T> | IOptionNone. |
Option.ts |
IOptionSome (type) |
Present value (isSome: true, isNone: false, value: T). |
Option.ts |
IOptionNone (type) |
Absent value (isSome: false, isNone: true). |
Option.ts |
Async carriers
Section titled “Async carriers”| Export | Description | Source |
|---|---|---|
AsyncResult (type) |
Lazy thunk { readonly run: () => Promise<IResultOfT<T, E>> }; E defaults to unknown. |
AsyncResult.ts |
AsyncOption (type) |
Lazy thunk { readonly run: () => Promise<IOption<T>> }. |
AsyncOption.ts |
Runtime entry marker
Section titled “Runtime entry marker”| Export | Description | Source |
|---|---|---|
default |
Empty object. Its only purpose is to make Rolldown materialize the root and /types entries and their sourcemaps; it has no domain behavior. |
index.ts |
Contract notes
Section titled “Contract notes”- Results and options are plain objects — no classes, no prototype methods, no sentinels.
- Narrow with the discriminant:
isSuccess/isFailureon Results,isSome/isNoneon Options. Accessingvalueon a failure orerroron a success is a compile-time error. - Every property is
readonly; values are immutable by contract. TErrordefaults tounknown, notError: a bare failure carries no usable payload until the caller narrows it.- The root barrel
@sandlada/resultand@sandlada/result/typesexport the same contract set; both are type-focused, and functional runtime values come from subpaths. - Values survive
JSON.stringifyunchanged, including both discriminants:
JSON.stringify(ok(42)); // '{"isSuccess":true,"isFailure":false,"value":42}'JSON.stringify(err('x')); // '{"isSuccess":false,"isFailure":true,"error":"x"}'JSON.stringify(ofSome(1)); // '{"isSome":true,"isNone":false,"value":1}'JSON.stringify(ofNone()); // '{"isSome":false,"isNone":true}'Design notes
Section titled “Design notes”- Pure discriminated unions over classes. Plain objects match TypeScript’s structural type system, serialize trivially, and support property narrowing without
instanceof. - Generic
TErrorover a hardcoded error type. Consumers define their own error contract; the library never converts a domain error intoErroron their behalf. - Type-focused barrel. The root entry exports contracts plus the empty
defaultmarker only. Functional runtime values are reachable exclusively through subpaths, which resolves name collisions at the import site and keeps tree-shaking total.