# @decs/typeschema 中文文档

规范地址：https://npmcn.com/package/%40decs/typeschema/

版本：0\.12\.2
npm 元数据更新时间：2024-02-22T07:01:26.726Z
中文译文更新时间：2026-11-11T19:30:26.008Z
软件包许可：MIT

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

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

弃用说明：TypeSchema has been restructured\. Install @typeschema/main or @typeschema/all instead\.

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

- 包体积（解压后）：56.4 kB，7 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@deepkit/type ^1\.0\.1-alpha\.113，@effect/schema ^0\.60\.6，@sinclair/typebox ^0\.32\.11，ajv ^8\.12\.0，arktype ^1\.0\.29-alpha，effect ^2\.1\.2，fp-ts ^2\.16\.2，io-ts ^2\.2\.21，joi ^17\.12\.0，ow ^0\.28\.2，runtypes ^6\.7\.0，superstruct ^1\.0\.3，valibot ^0\.26\.0，vite ^5\.0\.12，yup ^1\.3\.3，zod ^3\.22\.4
- 首次发布：2023-07-01T23:12:54.239Z
- 最新版本发布：2024-01-20T22:57:26.300Z
- 近 30 天下载量：13603（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：42（近 12 个月 0 个）

## 中文简介

用于设计验证的通用适配器

## 安装

```sh
npm install @decs/typeschema
```

也可以使用：`pnpm add @decs/typeschema`、`yarn add @decs/typeschema`、`bun add @decs/typeschema`

## README

  

![TypeSchema](https://npmcn.com/package/%40decs/typeschema/assets/logo.png)

# 类型方案

✨ [https://typeschema.com (中文(简体) )](https://typeschema.com/) ✨  
用于设计验证的通用适配器

  

[![License](https://img.shields.io/github/license/decs/typeschema)](https://opensource.org/licenses/MIT) [![Bundle size](https://img.shields.io/bundlephobia/minzip/%40decs%2Ftypeschema)](https://bundlephobia.com/package/@decs/typeschema) [![npm downloads](https://img.shields.io/npm/dw/@decs/typeschema.svg)](https://www.npmjs.com/package/@decs/typeschema) [![GitHub stars](https://img.shields.io/github/stars/decs/typeschema)](https://github.com/decs/typeschema/stargazers)

[设置](https://npmcn.com/package/%40decs/typeschema/#readme-setup)   二. 支助   [API 密码](https://npmcn.com/package/%40decs/typeschema/#readme-api)   二. 支助   [覆盖范围](https://npmcn.com/package/%40decs/typeschema/#readme-coverage)   二. 支助   [GitHub 图像](https://github.com/decs/typeschema)   二. 支助   [无](https://www.npmjs.com/package/@decs/typeschema)   二. 支助   [德诺](https://deno.land/x/typeschema)

  

许多图书馆依赖于某种类型的验证. 他们的维护者可以选择:

1.  ⁠**执行自己的** 验证逻辑:这会导致更多的代码需要维护,我们那里已经有许多好的解决方案(例如. [`zod`](https://zod.dev/), (中文(简体) ), [`arktype`](https://arktype.io/), (中文(简体) ), [`typia`](https://typia.io/)(中文(简体) )
2.  **两人的密码** 带有特定验证库: 限制使用另一个软件的开发者采用
3.  **支持多个** 验证库:这是不断更新(如. [tRPC 主题](https://trpc.io/)(中文(简体) )

因为总有取舍,所以没有最好的验证库. 每个开发者选择对他们最有意义的库. TypeSchema通过方便提供选项3来解决这个问题: **支持多个校验库外出.**

## 特征

-   从计划验证库解析
-   小客户足迹,可树摇
-   方便使用,最小API

## 使用量

```ts
import type {Infer, InferIn, Schema} from '@decs/typeschema';
import {assert, validate, wrap} from '@decs/typeschema';

// Use your favorite validation library, e.g. `zod`, `arktype`, `typia`
const schema: Schema = z.string();
const schema: Schema = type('string');
const schema: Schema = typia.createAssert<string>();

// Extracts the schema type
type Output = Infer<typeof schema>; // `string`
type Input = InferIn<typeof schema>; // `string`

// Returns the wrapped schema with access to all its operations
const wrapped = wrap(schema);
await wrapped.validate('123'); // {success: true, data: '123'}
await wrapped.assert('123'); // '123'

// Returns the validated data or a list of `ValidationIssue`s
await validate(schema, '123'); // {success: true, data: '123'}
await validate(schema, 123); // {success: false, issues: [`ValidationIssue`]}

// Returns the validated data or throws an `AggregateError`
await assert(schema, '123'); // '123'
await assert(schema, 123); // throws `AggregateError`
```

#### tRPC 主题

您可以使用任何支持的计划 [tRPC 主题](https://trpc.io/) 通过 `wrap` 函数 :

```ts
import {wrap} from '@decs/typeschema';
import {initTRPC} from '@trpc/server';
import {object, string} from 'valibot';

// Use your favorite validation library, e.g. `valibot`
const schema = object({name: string()});

const t = initTRPC.create();
const appRouter = t.router({
  hello: t.procedure
    .input(wrap(schema)) // like this
    .query(({input}) => `Hello, ${input.name}!`),
});
```

## 覆盖范围

TypeSchema 支持所有主要的计划验证库 :

| 项目 | 大众性 | `wrap` | `validate`  
`assert` | `Infer` | `InferIn` | 示例计划 |
| :-- | :-: | :-: | :-: | :-: | :-: | :-- |
| [佐德语](https://zod.dev/) | [![GitHub stars](https://img.shields.io/github/stars/colinhacks/zod?style=social)](https://github.com/colinhacks/zod) | ✅ | ✅ | ✅ | ✅ | `z.string()` |
| [对](https://github.com/jquense/yup) | [![GitHub stars](https://img.shields.io/github/stars/jquense/yup?style=social)](https://github.com/jquense/yup) | ✅ | ✅ | ✅ | ✅ | `string()` |
| [乔伊](https://joi.dev/) | [![GitHub stars](https://img.shields.io/github/stars/hapijs/joi?style=social)](https://github.com/hapijs/joi) | ✅ | ✅ | ❌ | ❌ | `Joi.string()` |
| [页:1](https://ajv.js.org/) | [![GitHub stars](https://img.shields.io/github/stars/ajv-validator/ajv?style=social)](https://github.com/ajv-validator/ajv) | ✅ | ✅ | ❌ | ❌ | `{type: "string"}` |
| [超级结构](https://docs.superstructjs.org/) | [![GitHub stars](https://img.shields.io/github/stars/ianstormtaylor/superstruct?style=social)](https://github.com/ianstormtaylor/superstruct) | ✅ | ✅ | ✅ | ❌ | `string()` |
| [io-ts(双胞胎)](https://gcanti.github.io/io-ts) | [![GitHub stars](https://img.shields.io/github/stars/gcanti/io-ts?style=social)](https://github.com/gcanti/io-ts) | ✅ | ✅ | ✅ | ✅ | `t.string` |
| [华丽机器人](https://valibot.dev/) | [![GitHub stars](https://img.shields.io/github/stars/fabian-hiller/valibot?style=social)](https://github.com/fabian-hiller/valibot) | ✅ | ✅ | ✅ | ✅ | `string()` |
| [类型框](https://github.com/sinclairzx81/typebox) | [![GitHub stars](https://img.shields.io/github/stars/sinclairzx81/typebox?style=social)](https://github.com/sinclairzx81/typebox) | ✅ | ✅ | ✅ | ✅ | `Type.String()` |
| [伤寒](https://typia.io/) | [![GitHub stars](https://img.shields.io/github/stars/samchon/typia?style=social)](https://github.com/samchon/typia) | ✅ | ✅ | ✅ | ✅ | `typia.createAssert<string>()` |
| [呜](https://sindresorhus.com/ow)\[^1\] | [![GitHub stars](https://img.shields.io/github/stars/sindresorhus/ow?style=social)](https://github.com/sindresorhus/ow) | ✅ | ✅ | ✅ | ✅ | `ow.string` |
| [效果](https://effect.website/) | [![GitHub stars](https://img.shields.io/github/stars/effect-ts/effect?style=social)](https://github.com/Effect-TS/effect) | ✅ | ✅ | ✅ | ✅ | `S.string` |
| [方舟类型](https://arktype.io/) | [![GitHub stars](https://img.shields.io/github/stars/arktypeio/arktype?style=social)](https://github.com/arktypeio/arktype) | ✅ | ✅ | ✅ | ✅ | `type('string')` |
| [深层](https://deepkit.io/) | [![GitHub stars](https://img.shields.io/github/stars/deepkit/deepkit-framework?style=social)](https://github.com/deepkit/deepkit-framework) | ✅ | ✅ | ❌ | ❌ | `typeOf<string>()` |
| [运行类型](https://github.com/pelotom/runtypes) | [![GitHub stars](https://img.shields.io/github/stars/pelotom/runtypes?style=social)](https://github.com/pelotom/runtypes) | ✅ | ✅ | ✅ | ✅ | `String` |

\[^1\]:用于 [呜](https://sindresorhus.com/ow),仅支持 v0.28.2 (sindresorhus/ow#248)

自定义验证也被支持 :

```ts
export function assertString(data: unknown): string {
  if (typeof data !== 'string') {
    throw new Error('Expected a string, got: ' + data);
  }
  return data;
}

await validate(assertString, '123'); // {success: true, data: '123'}
await validate(assertString, 123); // {success: false, issues: [`ValidationIssue`]}

await assert(assertString, '123'); // '123'
await assert(assertString, 123); // throws `AggregateError`
```

## 设置

用您选择的软件包管理器安装 TypeSchema :

<table><tbody><tr><th>无</th><td><code>npm install @decs/typeschema</code></td></tr><tr><th>雅伦</th><td><code>yarn add @decs/typeschema</code></td></tr><tr><th>小时</th><td><code>pnpm add @decs/typeschema</code></td></tr><tr><th>德诺</th><td><code>https://deno.land/x/typeschema</code></td></tr></tbody></table>

## API 密码

#### 类型

-   `Schema`
    
    计划通用接口  
    由所有支持的图书馆组成的计划类型联盟
    
-   `TypeSchema<TOutput, TInput = TOutput>`
    
    包扎图的接口,显示其所有操作
    
-   `Infer<TSchema extends Schema>`
    
    提取图案的输出类型
    
-   `InferIn<TSchema extends Schema>`
    
    提取策略的输入类型
    
-   `ValidationIssue`
    
    验证问题通用接口  
    包括a `message` 和可选 `path`
    

#### 职能

-   `wrap(schema)`
    
    ```ts
    wrap<TSchema extends Schema>(
      schema: TSchema,
    ): TypeSchema<Infer<TSchema>, InferIn<TSchema>>
    ```
    
    返回可访问其所有操作的包裹计划
    
-   `validate(schema, data)`
    
    ```ts
    validate<TSchema extends Schema>(
      schema: TSchema,
      data: unknown,
    ): Promise<ValidationResult<Infer<TSchema>>>
    ```
    
    返回已验证的数据或列表 `ValidationIssue`编号
    
-   `assert(schema, data)`
    
    ```ts
    assert<TSchema extends Schema>(
      schema: TSchema,
      data: unknown,
    ): Promise<Infer<TSchema>>
    ```
    
    返回已验证的数据或丢出一个 `AggregateError`
    

## 鸣谢

-   灵感来自 [tRPC 主题](https://trpc.io/)是因为 [输入输出验证符( o)](https://trpc.io/docs/server/validators)
-   由 [@ ecyrbe (英语)](https://github.com/ecyrbe)是因为 [建议](https://github.com/decs/typeschema/issues/1)
-   API 定义灵感来自 [@colinhacks (英语)](https://github.com/colinhacks)是因为 [建议](https://twitter.com/colinhacks/status/1634284724796661761)
-   日志设计 [平方厘米](https://www.flaticon.com/)

## 常见问题

### @decs/typeschema 是什么？

用于设计验证的通用适配器注意：该包已被作者标记为弃用——TypeSchema has been restructured\. Install @typeschema/main or @typeschema/all instead\.

### 如何安装 @decs/typeschema？

在项目目录运行 npm install @decs/typeschema，或使用 pnpm add / yarn add / bun add。

### @decs/typeschema 支持 TypeScript 吗？

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

### @decs/typeschema 支持 ESM 还是 CommonJS？

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

### @decs/typeschema 可以商用吗？

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

### @decs/typeschema 的下载量有多少？

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

### @decs/typeschema 还在维护吗？

最新版本 0\.12\.2 发布于 2024/01/21（2 年前）；至今共发布 42 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/07/02。作者已将其标记为弃用，新项目建议寻找替代方案。

