TypeBox 0.34.x
Json Schema Type Builder with Static Type Resolution for TypeScript
[](https://badge.fury.io/js/%40sinclair%2Ftypebox) [](https://www.npmjs.com/package/%40sinclair%2Ftypebox) [](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml) [](https://opensource.org/licenses/MIT)
Install bash $ npm install @sinclair/typebox # TypeBox-Legacy | 0.34.x $ npm install typebox # TypeBox | 1.0.x Example typescript import { Type, type Static } from ‘@sinclair/typebox’ const T = Type.Object({ // const T = { x: Type.Number(), // type: ‘object’, y: Type.Number(), // required: [‘x’, ‘y’, ‘z’], z: Type.Number() // properties: { }) // x: { type: ‘number’ }, // y: { type: ‘number’ }, // z: { type: ‘number’ } // } // } type T = Static
// type T = { // x: number, // y: number, // z: number // }
Overview > ⚠️ TypeBox versions (pre-1.0) will continue active maintenance through 2026 and beyond. This repository services as the OIDC publishing environment for the `@sinclair/typebox` package scope on NPM. For TypeBox versions 1.0 and above, refer to https://github.com/sinclairzx81/typebox TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation. This library is designed to allow Json Schema to compose similar to how types compose within TypeScript’s type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire. License MIT Contents – [Install](#install) – [Overview](#overview) – [Usage](#usage) – [Types](#types) – [Json](#types-json) – [JavaScript](#types-javascript) – [Options](#types-options) – [Properties](#types-properties) – [Generics](#types-generics) – [Recursive](#types-recursive) – [Modules](#types-modules) – [Template Literal](#types-template-literal) – [Indexed](#types-indexed) – [Mapped](#types-mapped) – [Conditional](#types-conditional) – [Transform](#types-transform) – [Guard](#types-guard) – [Unsafe](#types-unsafe) – [Values](#values) – [Assert](#values-assert) – [Create](#values-create) – [Clone](#values-clone) – [Check](#values-check) – [Convert](#values-convert) – [Default](#values-default) openclaw docker 教程- [Clean](#values-clean) – [Cast](#values-cast) – [Decode](#values-decode) – [Encode](#values-decode) – [Parse](#values-parse) – [Equal](#values-equal) – [Hash](#values-hash) – [Diff](#values-diff) – [Patch](#values-patch) – [Errors](#values-errors) – [Mutate](#values-mutate) – [Pointer](#values-pointer) – [Syntax](#syntax) – [Create](#syntax-create) – [Parameters](#syntax-parameters) – [Generics](#syntax-generics) – [Options](#syntax-options) – [NoInfer](#syntax-no-infer) – [TypeRegistry](#typeregistry) – [Type](#typeregistry-type) – [Format](#typeregistry-format) – [TypeCheck](#typecheck) – [Ajv](#typecheck-ajv) – [TypeCompiler](#typecheck-typecompiler) – [TypeMap](#typemap) – [Usage](#typemap-usage) – [TypeSystem](#typesystem) – [Policies](#typesystem-policies) – [Error Function](#error-function) – [Workbench](#workbench) – [Codegen](#codegen) – [Ecosystem](#ecosystem) – [Benchmark](#benchmark) – [Compile](#benchmark-compile) – [Validate](#benchmark-validate) – [Compression](#benchmark-compression) – [Contribute](#contribute)
Usage The following shows general usage. typescript import { Type, type Static } from ‘@sinclair/typebox’ //——————————————————————————————– // // Let’s say you have the following type … // //——————————————————————————————– type T = { id: string, name: string, timestamp: number } //——————————————————————————————– // // … you can express this type in the following way. // //——————————————————————————————– const T = Type.Object({ // const T = { id: Type.String(), // type: ‘object’, name: Type.String(), // properties: { timestamp: Type.Integer() // id: { }) // type: ‘string’ // }, // name: { // type: ‘string’ // }, // timestamp: { // type: ‘integer’ // } // }, // required: [ // ‘id’, // ‘name’, // ‘timestamp’ // ] // } //——————————————————————————————– // // … then infer back to the original static type this way. // //——————————————————————————————– type T = Static
// type T = { // id: string, // name: string, // timestamp: number // } //——————————————————————————————– // // … or use the type to parse JavaScript values. // //——————————————————————————————– import { Value } from ‘@sinclair/typebox/value’ const R = Value.Parse(T, value) // const R: { // id: string, // name: string,
发布者:Ai探索者,转载请注明出处:https://javaforall.net/284564.html原文链接:https://javaforall.net
