通过转换实现超快/超简单的运行时验证器和序列化器
…typia 是一个支持以下功能的 Transformer 库:
[!NOTE]
- 只需一行代码,使用纯 TypeScript 类型
- 运行时验证器比
class-validator快 20,000 倍- JSON 序列化比
class-Transformer快 200 倍- LLM 函数调用工具将准确率从 6.75% 提升至 100%
在左侧编写类型,typia 会在构建时将其编译为右侧的验证器——无需 schema,也无需运行时反射。
使用 ttsc 工具链安装 typia。
# install
npm i typia
npm i -D ttsc typescript
# build
npx ttsc
# run a script directly
npx ttsx src/index.ts你必须使用 ttsc 和 ttsx。原生的 tsc、ts-node 和 tsx 无法应用 typia 转换,因此它们无法工作。
关于打包工具集成(Vite、Next.js、Webpack、Rollup、esbuild 等),请使用 @ttsc/unplugin。
如果你调用 typia 函数,它会被编译成如下形式。
这是 typia 的核心概念:将 TypeScript 类型转换为运行时函数。typia.is<T>() 函数会在编译层面分析目标类型 T,从而被转换为一个专用的类型检查器。
此功能使开发者能够在其的应用程序中确保类型安全,利用 TypeScript 的静态类型,同时还提供运行时验证。无需定义额外的 schema,你可以直接使用纯 TypeScript 类型本身。
//----
// examples/checkString.ts
//----
import typia, { tags } from "typia";
export const checkString = typia.createIs<string>();
//----
// examples/checkString.js
//----
import typia from "typia";
export const checkString = (() => {
return (input) => "string" === typeof input;
})();感谢你的支持。
你的捐赠是对 typia 开发的鼓励。
你可以体验
暂无开放 Issues,或尚未同步最近议题。