@collidor/result

中文文档
1.0.5发布于 2026/08/16MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7753

A lightweight, type-safe Result pattern implementation for robust error handling.

原始使用文档
文档目录9 节
  1. Installation
  2. Basic Usage
  3. Features
  4. Creating Results
  5. Working with Results
  6. Functional Piping
  7. API Documentation
  8. Types
  9. Result Static Methods

Codecov npm version License: MIT

Result

A lightweight, type-safe Result pattern implementation for robust error handling in TypeScript.

  • 🚀 Zero dependencies
  • 🛡️ Type-safe error handling
  • 🔄 Functional piping (sync/async)
  • 🛠️ Rich utility set (map, chain, combine, etc.)
  • 🌐 Boundary-safe check for cross-context results

Installation

npm install @collidor/result

Basic Usage

import { Result } from "@collidor/result";

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return Result.err("Division by zero");
  }
  return Result.ok(a / b);
}

const res = divide(10, 2);
if (res.success) {
  console.log(res.value); // 5
} else {
  console.error(res.error); // "Division by zero"
}

Features

Creating Results

  • Result.ok(value): Creates a successful result.
  • Result.err(error): Creates a failure result.
  • Result.none(): Shortcut for a failed result with null error.
  • Result.try(() => ...): Wraps a function that might throw.
  • Result.from(value | Error): Converts a value or Error object to a Result.
  • Result.fromPromise(promise): Wraps a promise, catching rejections into a Result.

Working with Results

  • Result.isResult(thing): Boundary-safe check if a value is a Result object.
  • Result.unwrap(result): Returns the value or throws a re-hydrated Error.
  • Result.getOrElse(result, defaultValue): Returns value or a default.
  • Result.map(result, fn): Transforms the value if successful.
  • Result.chain(result, fn): Chains another Result-returning function.
  • Result.combine(results[]): Combines multiple results into one (all-or-nothing).

Functional Piping

Piping allows you to chain operations cleanly:

const result = Result.pipe(
  Result.ok(10),
  (n) => n * 2,
  (n) => Result.ok(n + 5),
  (n) => n.toString()
);
// Result.ok("25")

For asynchronous operations:

const result = await Result.pipeAsync(
  Result.ok(10),
  async (n) => n * 2,
  (n) => Promise.resolve(Result.ok(n + 5))
);

API Documentation

Types

  • Result<T, E>: The main type, union of Ok<T, E> and Err<E, T>.
  • Unwrap<T>: Helper type to infer the value type from a Result or Promise<Result>.

Result Static Methods

MethodDescription
ok(value?)Creates an Ok result.
err(error)Creates an Err result.
none()Creates a failed result with null error.
isResult(thing)Checks if value is a Result object.
from(val)Converts value or Error to Result.
fromPromise(p)Wraps a promise into a Result.
unwrap(res)Gets value or throws re-hydrated Error.
map(res, fn)Transforms value if success.
chain(res, fn)FlatMap for Results.
getOrElse(res, def)Gets value or returns default.
try(fn)Executes fn and catches any error into Result.
pipe(res, ...ops)Functional composition for Results.
pipeAsync(res, ...ops)Async functional composition.
combine(results)Turns Result[] into Result<any[]>.

Contribution

  1. Fork repository
  2. Create feature branch
  3. Commit changes
  4. Push to branch
  5. Create new Pull Request

License

MIT © Alykam Burdzaki

版本与发布记录

@collidor/result 共发布 4 个版本,首次发布于 2025/09/27;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.5latest2026/08/162 个月前
0.0.32025/12/1410 个月前
0.0.22025/09/271.1 年前
0.0.12025/09/271.1 年前

关于 @collidor/result 的常见问题

@collidor/result 是什么?

A lightweight, type-safe Result pattern implementation for robust error handling.

如何安装 @collidor/result?

在项目目录运行 npm install @collidor/result,或使用 pnpm add / yarn add / bun add。

@collidor/result 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@collidor/result 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@collidor/result 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@collidor/result 可以商用吗?

@collidor/result 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@collidor/result 的下载量有多少?

根据 npm 官方下载统计,@collidor/result 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,753 次,日均约 258 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@collidor/result 还在维护吗?

最新版本 1.0.5 发布于 2026/08/16(2 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/09/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。