# 与 TypeScript 一起使用
¥Using with TypeScript
# 附加功能
¥Additional functionality
Ajv 利用 TypeScript 类型系统提供 JavaScript 中无法实现的附加功能:
¥Ajv takes advantage of TypeScript type system to provide additional functionality that is not possible in JavaScript:
工具类型
JSONSchemaType
和JTDSchemaType
将数据类型转换为结构类型,以简化结构的编写,适用于 JSON 结构(但不支持联合)和 JSON 类型定义(支持标记联合)。¥utility types
JSONSchemaType
andJTDSchemaType
to convert data type into the schema type to simplify writing schemas, both for JSON Schema (but without union support) and for JSON Type Definition (with tagged unions support).工具类型
JTDDataType
将 JSON 类型定义结构转换为其定义的数据类型。¥utility type
JTDDataType
to convert JSON Type Definition schema into the type of data that it defines.已编译的校验函数是类型保护,可在成功校验后缩小类型范围。
¥compiled validation functions are type guards that narrow the type after successful validation.
JSON 结构的校验错误被定义为标记联合,以进行类型安全的错误处理。
¥validation errors for JSON Schema are defined as tagged unions, for type-safe error handling.
当使用工具类型时,编译的 JTD 序列化器仅接受正确类型的数据(因为它们不校验数据是否有效),并且编译的解析器返回正确的数据类型。
¥when utility type is used, compiled JTD serializers only accept data of correct type (as they do not validate that the data is valid) and compiled parsers return correct data type.
# 结构的工具类型
¥Utility types for schemas
对于与 入门 中相同的示例:
¥For the same example as in Getting started:
确保 strictNullChecks 为 true
¥ensure strictNullChecks is true
有关高级示例,请参阅 这次测试 (opens new window)。
¥See this test (opens new window) for an advanced example.
# JTD 数据类型的工具类型
¥Utility type for JTD data type
你可以使用 JTD 结构来使用工具类型 JTDDataType
构造数据类型
¥You can use JTD schema to construct the type of data using utility type JTDDataType
TypeScript limitation
Note that it's currently not possible for JTDDataType
to know whether the compiler is inferring timestamps as strings or Dates, and so it conservatively types any timestamp as string | Date
. This is accurate, but often requires extra validation on the part of the user to confirm they're getting the appropriate data type.
# Type-safe error handling
With both JSON Schema and JSON Type Definition, the validation error type is an open union, but it can be cast to tagged unions (using validation keyword as tag) for easier error handling.
Continuing the example above:
# Type-safe parsers and serializers
With typescript, your compiled parsers and serializers can be type-safe, either taking their type from schema type or from type parameter passed to compilation functions.
This example uses the same data and schema types as above:
# 类型安全联合
¥Type-safe unions
JSON 类型定义仅支持标记联合,因此 JTD 中的联合完全支持 JTDSchemaType
和 JTDDataType
。JSON Schema 更复杂,因此 JSONSchemaType
对类型安全联合的支持有限。
¥JSON Type Definition only supports tagged unions, so unions in JTD are fully supported for JTDSchemaType
and JTDDataType
.
JSON Schema is more complex and so JSONSchemaType
has limited support for type safe unions.
JSONSchemaType
将类型检查联合,其中每个联合元素被完全指定为 anyOf
数组或 oneOf
数组的元素。此外,如果基元的联合组合成数组 type
,则它们将进行适当的类型检查,例如 {type: ["string", "number"]}
。
¥JSONSchemaType
will type check unions where each union element is fully specified as an element of an anyOf
array or oneOf
array.
Additionally, unions of primitives will type check appropriately if they're combined into an array type
, e.g. {type: ["string", "number"]}
.
TypeScript 限制
请注意,由于 TypeScript 当前的限制,JSONSchemaType 无法校验联合的每个元素是否都存在,并且以下示例仍然有效 const schema: JSONSchemaType<number | string> = {type: "string"}
。
¥Note that due to current limitation of TypeScript, JSONSchemaType cannot verify that every element of the union is present, and the following example is still valid const schema: JSONSchemaType<number | string> = {type: "string"}
.
下面是一个更详细的示例,显示了几种联合类型:
¥Here's a more detailed example showing several union types:
```