![]() JSON Type Definition only supports tagged unions, so unions in JTD are fully supported for JTDSchemaType and JTDDataType. This example uses the same data and schema types as above: # Type-safe unions 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. 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.Ĭontinuing the example above: # Type-safe parsers and serializers This is accurate, but often requires extra validation on the part of the user to confirm they're getting the appropriate data type. 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. You can use JTD schema to construct the type of data using utility type JTDDataType See this test (opens new window) for an advanced example. 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.įor the same example as in Getting started:.validation errors for JSON Schema are defined as tagged unions, for type-safe error handling. ![]() compiled validation functions are type guards that narrow the type after successful validation.utility type JTDDataType to convert JSON Type Definition schema into the type of data that it defines.utility types JSONSchemaType and JTDSchemaType 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).Ajv takes advantage of TypeScript type system to provide additional functionality that is not possible in JavaScript: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |