@cprussin/option-result

中文文档
2.0.1发布于 2026/04/23MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 21万

Yet another Typescript clone of the rust Option & Result enums.

原始使用文档
文档目录5 节
  1. Example
  2. Motivation
  3. Why use Rust's Option and Result instead of Haskell's Maybe…
  4. Classes
  5. Functions

@cprussin/option-result v2.0.1


@cprussin/option-result v2.0.1

This package contains yet another Typescript clone of the rust Option & Result enums.

Installing

Use the package manager of your choice to install:

  • npm: npm install --save-dev @cprussin/option-result
  • pnpm: pnpm add -D @cprussin/option-result
  • yarn: yarn add -D @cprussin/option-result

Usage

This library mostly implements an as-close-to-identical API to the Rust types as possible.

Example

import { Some } from "@cprussin/option-result";
Some(5)
  .map((value) => value + 1)
  .match({
    Some: (value) => console.log(value),
    None: () => console.error("There's nothing here!"),
  });

FAQ

Motivation

But why?

Two main reasons:

  1. I didn't find any existing implementations I found to be satisfactory. They either lacked the strong typing guarantees I wanted, or they were incomplete and unmaintained, or I simply didn't like their stylistic preferences. For some reason or another I wasn't happy with any of the other implementations I tried.
  2. It's fun. Type-safe Option and Result are really rewarding types to implement. If you haven't tried building them yourself, I strongly recommend it.

Why use Rust's Option and Result instead of Haskell's Maybe and Either?

I opted to implement the Rust versions of these types and not the Haskell/Purescript versions for a few reasons:

  1. There are no type classes in Typescript, so approximated Maybe and Either implementations would have way more differences from their source versions than Option and Result instances.
  2. I love Haskell and Purescript (and other ML family languages) personally, but I also believe those to be far less approachable languages than Rust. I think the Rust versions are easier to understand and teach and the terminology is easier to grok for newcomers, at the expense of some degree of generic code. I use this library practically myself and while I value type safety, I also value approachability from developers other than myself, and I believe the Rust versions strike a better balance there than the Haskell/Purescript ones do.
  3. Purescript compiles to Javascript; as such I don't believe there's much value in cloning types from that language when you could just as easily write parts of your application in Purescript directly instead and not have to deal with the shortcomings of using those types in a language not designed for them.

Classes

Functions

版本与发布记录

@cprussin/option-result 共发布 13 个版本,首次发布于 2023/05/05;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.1latest2026/04/236 个月前
2.0.02025/06/181.4 年前
1.4.22025/04/011.6 年前
1.4.12025/03/121.7 年前
1.4.02025/03/121.7 年前
1.3.12024/06/032 年前

关于 @cprussin/option-result 的常见问题

@cprussin/option-result 是什么?

Yet another Typescript clone of the rust Option & Result enums.

如何安装 @cprussin/option-result?

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

@cprussin/option-result 支持 TypeScript 吗?

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

@cprussin/option-result 支持 ESM 还是 CommonJS?

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

@cprussin/option-result 需要什么版本的 Node.js?

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

@cprussin/option-result 可以商用吗?

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

@cprussin/option-result 的下载量有多少?

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

@cprussin/option-result 还在维护吗?

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