@birchill/discriminator

中文文档
0.3.1发布于 2024/11/07MITTypeScriptESM + CJS零依赖近 30 天下载 1976

A tagged union type for superstruct based on JSON typedef's discriminator type

原始使用文档
文档目录6 节
  1. Why?
  2. Specifics
  3. Developing
  4. Building
  5. Testing
  6. Releasing

discriminator

A tagged union type for superstruct based on JSON typedef's discriminator type.

import { boolean, Infer, enums, string, object } from 'superstruct';
import { discriminator } from '@birchill/discriminator';

const schema = discriminator('eventType', {
  USER_CREATED: object({ id: string() }),
  USER_PAYMENT_PLAN_CHANGED: object({
    id: string(),
    plan: enums(['FREE', 'PAID']),
  }),
  USER_DELETED: object({ id: string(), softDelete: boolean() }),
});

type SchemaType = Infer<typeof schema>;

// SchemaType =
// {
//   eventType: "USER_CREATED";
//   id: string;
// } | {
//   eventType: "USER_PAYMENT_PLAN_CHANGED";
//   id: string;
//   plan: "FREE" | "PAID";
// } | {
//   eventType: "USER_DELETED";
//   id: string;
//   softDelete: boolean;
// }

Why?

  • If you try to model the above using union() objects and validation fails you get errors like:

    Expected the value to satisfy a union of ``object | object | object``, but received: [object Object].

    Using discriminator() you get errors like:

    At path: value.name -- Expected a string with a length between ``0``and ``256`` but received one with a length of ``257`` .

  • Better semantics.

  • Easier translation to and from JSON typedef should that be useful.

Specifics

discriminator() takes two parameters:

  1. A string representing the tagged union's tag field.
  2. An object where the keys are the tag values and the values are object(), type(), or discriminator() structs.

If you need to model a branch where there are no other properties just use an empty object() or type(). This is important because you're indicating whether or not that branch is allowed to have extra values on it (type()) or not (object()).

e.g.

discriminator('action', {
  signin: type({ email: string(), token: string() }),
  signout: type(),
});

You can nest discriminator() objects like so:

discriminator('result', {
  success: discriminator('task', {
    upload: type({
      filename: string(),
    }),
    download: type({
      filename: string(),
      bytes: number(),
    }),
  }),
  failure: type({
    code: number(),
  }),
});

// `Infer` here produces the type:
//
// {
//   result: "success";
//   task: "upload";
//   filename: string;
// } | {
//   result: "success";
//   task: "download";
//   filename: string;
//   bytes: number;
// } | {
//   result: "failure";
//   code: number;
// }

Developing

Building

pnpm build

Testing

pnpm test

Releasing

pnpm release-it

版本与发布记录

@birchill/discriminator 共发布 4 个版本,首次发布于 2021/08/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.1latest2024/11/072 年前
0.3.02023/10/123 年前
0.2.02022/09/204 年前
0.1.02021/08/265 年前

关于 @birchill/discriminator 的常见问题

@birchill/discriminator 是什么?

A tagged union type for superstruct based on JSON typedef's discriminator type

如何安装 @birchill/discriminator?

在项目目录运行 npm install @birchill/discriminator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:superstruct(>0.16.0 <3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@birchill/discriminator 支持 TypeScript 吗?

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

@birchill/discriminator 支持 ESM 还是 CommonJS?

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

@birchill/discriminator 需要什么版本的 Node.js?

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

@birchill/discriminator 可以商用吗?

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

@birchill/discriminator 的下载量有多少?

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

@birchill/discriminator 还在维护吗?

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