# @collidor/result 中文文档

规范地址：https://npmcn.com/package/%40collidor/result/

版本：1\.0\.5
npm 元数据更新时间：2026-08-16T12:28:28.460Z
中文译文更新时间：2026-11-10T11:10:11.233Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40collidor/result
原文对照：https://npmcn.com/package/%40collidor/result/?lang=original
项目主页：https://github.com/collidor/result#readme
源码仓库：https://github.com/collidor/result

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：57.3 kB，17 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=24\.0\.0
- 运行依赖：0 个
- 首次发布：2025-09-27T06:17:47.960Z
- 最新版本发布：2026-08-16T12:28:28.153Z
- 近 30 天下载量：7753（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 2 个）

## 中文简介

轻量级,类型安全的结果模式执行,用于强力处理出错.

## 安装

```sh
npm install @collidor/result
```

也可以使用：`pnpm add @collidor/result`、`yarn add @collidor/result`、`bun add @collidor/result`

## README

[![Codecov](https://codecov.io/gh/collidor/collidor/branch/main/graph/badge.svg?flag=result)](https://codecov.io/gh/collidor/collidor) [![npm version](https://img.shields.io/npm/v/@collidor/result)](https://www.npmjs.com/package/@collidor/result) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# 结果

轻而易举、安全 `Result` 在 TypeScript 中执行强性错误处理模式.

-   零依赖关系
-   QQ 类型安全错误处理
-   \* 功能管道(同步/同步)
-   \* 丰富的工具集(图、链、组合等)
-   \* 跨文本结果的边界安全检查

## 安装

```bash
npm install @collidor/result
```

## 基本用途

```typescript
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"
}
```

## 特征

### 创造结果

-   `Result.ok(value)`: 创建成功结果.
-   `Result.err(error)`: 创建失败结果.
-   `Result.none()`: 失败结果的快捷键 `null` 错误.
-   `Result.try(() => ...)`: 环绕可能丢出的一个函数.
-   `Result.from(value | Error)`: 将一个值或出错对象转换为结果.
-   `Result.fromPromise(promise)`:兑现诺言,将拒绝结果变成结果.

### 与成果合作

-   `Result.isResult(thing)`: 边界安全检查一个值是否是结果对象.
-   `Result.unwrap(result)`: 返回值或丢出再水合错误.
-   `Result.getOrElse(result, defaultValue)`: 返回值或默认值.
-   `Result.map(result, fn)`:如果成功,则转换值.
-   `Result.chain(result, fn)`: 链条另一个结果返回函数.
-   `Result.combine(results[])`:将多个结果合并为一个结果(全结果或无结果).

### 功能管道

Piping 允许您将操作连锁:

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

对于同步操作:

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

## API 文档

### 类型

-   `Result<T, E>`主要类型: `Ok<T, E>` 财务报告和审定财务报表 `Err<E, T>`。 。 。 .
-   `Unwrap<T>`: 帮助键从 a 推断值类型 `Result` 或者说 `Promise<Result>`。 。 。 .

### 结果静态方法

| 方法 | 说明 |
| --- | --- |
| `ok(value?)` | 创建 OK 结果 . |
| `err(error)` | 创建错误结果. |
| `none()` | 创建失败的结果 `null` 错误. |
| `isResult(thing)` | 检查值是否为结果对象. |
| `from(val)` | 将值或出错转换为结果 . |
| `fromPromise(p)` | 将承诺写入结果. |
| `unwrap(res)` | 获取值或丢出再水合错误. |
| `map(res, fn)` | 如果成功, 则转换值 . |
| `chain(res, fn)` | FlatMap for Results. (原始内容存档于2018-09-27). |
| `getOrElse(res, def)` | 获取值或返回默认值. |
| `try(fn)` | 执行 fn 并捕捉出任何错误 . |
| `pipe(res, ...ops)` | 成果的职能构成. |
| `pipeAsync(res, ...ops)` | Async功能构成. |
| `combine(results)` | 转弯 `Result[]` 输入 `Result<any[]>`。 。 。 . |

# 捐款

1.  分叉仓库
2.  创建特性分支
3.  提交更改
4.  推向分支
5.  创建新的拉请求

# 许可证

麻省理工学院

## 常见问题

### @collidor/result 是什么？

轻量级,类型安全的结果模式执行,用于强力处理出错\.

### 如何安装 @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 和提交记录判断。

