Documentation
This commit is contained in:
BIN
design/concept.png
Normal file
BIN
design/concept.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 777 KiB |
BIN
design/javascript.jpg
Normal file
BIN
design/javascript.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
Binary file not shown.
@@ -1,28 +1,25 @@
|
||||
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
|
||||
import { TypeBox } from '@sinclair/typemap'
|
||||
|
||||
// Syntax Type
|
||||
import * as z from 'zod'
|
||||
|
||||
const S = `{
|
||||
x: number,
|
||||
y: number,
|
||||
z: number
|
||||
}`
|
||||
const Z = z.object({ // const Z: z.ZodObject<{
|
||||
x: z.number(), // x: z.ZodNumber;
|
||||
y: z.number(), // y: z.ZodNumber;
|
||||
z: z.number() // z: z.ZodNumber;
|
||||
}).strict() // }, "strict", ...>
|
||||
|
||||
const T = TypeBox(S) // const T: TObject<{
|
||||
// x: TNumber,
|
||||
// y: TNumber,
|
||||
// z: TNumber
|
||||
// }>
|
||||
// TypeBox represents types as Json Schema
|
||||
|
||||
const V = Valibot(S) // const V: ObjectSchema<{
|
||||
// x: NumberSchema<...>,
|
||||
// y: NumberSchema<...>,
|
||||
// z: NumberSchema<...>
|
||||
// }, ...>
|
||||
const T = TypeBox(Z) // const T = {
|
||||
// type: 'object',
|
||||
// required: ['x', 'y', 'z'],
|
||||
// additionalProperties: false,
|
||||
// properties: {
|
||||
// x: { type: 'number' },
|
||||
// y: { type: 'number' },
|
||||
// z: { type: 'number' }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
const Z = Zod(S) // const Z: ZodObject<{
|
||||
// x: ZodNumber,
|
||||
// y: ZodNumber,
|
||||
// z: ZodNumber
|
||||
// }, ...>
|
||||
console.log(T)
|
||||
192
readme.md
192
readme.md
@@ -24,25 +24,25 @@ $ npm install @sinclair/typemap --save
|
||||
|
||||
## Usage
|
||||
|
||||
Parse and Compile Types from TypeScript Syntax ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIABAZ2ADsBjDAQ2CgHoYBPMLERsUgFBxhI0WJEC6EGpXhQslAK4Z4AXhRowmLAApSsqLQDmcAD5wayjKQCUAOgAKjKJV2kAElgwYIcACpcWACyfLYCrKxigECk4rHRcQnx4ZGiMYmxaekJgCikgACk+XBOLjgAaowYiljJ8Vk1tWLVqfWieblwAIKyAFz+AKo0wNIAPADafgDKMIY0RgA0-gByVgC6AHzCjSJRm8KtcFIycgrKMD0GxmYWVnDqHl4+-oEh-EA))
|
||||
Parse and Compile Types from TypeScript Syntax ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApnAhgZzgBUBPMHAXzgDMo04ByAAX2ADsBjDXYKAehhksIXGAYAoOFOkzZ08Rwht88KFnwBXDPAC8xIQAoGKqOwDmcAD5w2WjAwCUAOgAKuKPixGAElgwYIOAB1aAwAE0dxPj5ZQCBSOQSpeMSUmXiomLTUuOzswBRSQABSIrg3DxwANVwMDSwMnNykhoS6rKbCguIAVTZgJQAeAG0iAGUYUzYzABpiADk7AF0APhbpWJWpdrhFZVV1LRgALjgTcysbOzg9Bl9-QJCocIYgA))
|
||||
|
||||
```typescript
|
||||
import { Compile } from '@sinclair/typemap'
|
||||
import { Compile as Type } from '@sinclair/typemap'
|
||||
|
||||
const result = Compile('string | null').Parse('Hello World')
|
||||
// │ │ │
|
||||
// │ │ └─── Parse Value
|
||||
// │ │
|
||||
// │ └── TUnion<[TString, TNull]>
|
||||
const result = Type('string | null').Parse('Hello World')
|
||||
// │ │ │
|
||||
// │ │ └─── Parse Value
|
||||
// │ │
|
||||
// │ └── TUnion<[TString, TNull]> | Abstract Type
|
||||
// │
|
||||
// └── const result: string | null = 'Hello World'
|
||||
```
|
||||
|
||||
## Overview
|
||||
|
||||
TypeMap is an syntax frontend and compiler backend for the [TypeBox](https://github.com/sinclairzx81/typebox), [Valibot](https://github.com/fabian-hiller/valibot) and [Zod](https://github.com/colinhacks/zod) libraries. It provides a common TypeScript syntax for type construction, a runtime compiler for high-performance validation and provides type translation from one library to another.
|
||||
TypeMap is a syntax frontend and compiler backend for the [TypeBox](https://github.com/sinclairzx81/typebox), [Valibot](https://github.com/fabian-hiller/valibot) and [Zod](https://github.com/colinhacks/zod) runtime type libraries. It offers a common TypeScript syntax for type construction, a runtime compiler for high-performance validation and type translation from one library to another.
|
||||
|
||||
TypeMap is written to be an advanced type translation system for the [TypeBox](https://github.com/sinclairzx81/typebox) project. It is built to integrate and accelerate remote type libraries on Json Schema compatible infrastructure as well as to integrate TypeBox into remote type library infrastructure via reverse type remapping. This project also provides high-performance validation for frameworks that orientate around the [Standard Schema](https://github.com/standard-schema/standard-schema) TypeScript interface.
|
||||
TypeMap is written as an advanced type translation system for the [TypeBox](https://github.com/sinclairzx81/typebox) project. It is designed to accelerate remote type libraries on TypeBox infrastructure as well as to integrate TypeBox into remote type library infrastructure via reverse type remapping. This project also provides high-performance validation for frameworks that orientate around the [Standard Schema](https://github.com/standard-schema/standard-schema) TypeScript interface.
|
||||
|
||||
License: MIT
|
||||
|
||||
@@ -52,30 +52,31 @@ License: MIT
|
||||
- [Usage](#Usage)
|
||||
- [Overview](#Overview)
|
||||
- [Example](#Example)
|
||||
- [Mapping](#Mapping)
|
||||
- [Syntax](#Syntax)
|
||||
- [TypeBox](#TypeBox)
|
||||
- [Valibot](#Valibot)
|
||||
- [Zod](#Zod)
|
||||
- [Syntax](#Syntax)
|
||||
- [Types](#Types)
|
||||
- [Options](#Options)
|
||||
- [Parameters](#Parameters)
|
||||
- [Generics](#Generics)
|
||||
- [Static](#Static)
|
||||
- [TreeShake](#TreeShake)
|
||||
- [Mapping](#Section-Mapping)
|
||||
- [Syntax](#Mapping-Syntax)
|
||||
- [TypeBox](#Mapping-TypeBox)
|
||||
- [Valibot](#Mapping-Valibot)
|
||||
- [Zod](#Mapping-Zod)
|
||||
- [Syntax](#Section-Syntax)
|
||||
- [Types](#Syntax-Types)
|
||||
- [Options](#Syntax-Options)
|
||||
- [Parameters](#Syntax-Parameters)
|
||||
- [Generics](#Syntax-Generics)
|
||||
- [Static](#Section-Static)
|
||||
- [Json Schema](#Json-Schema)
|
||||
- [Tree Shake](#Tree-Shake)
|
||||
- [Compile](#Compile)
|
||||
- [Benchmark](#Benchmark)
|
||||
- [Contribute](#Contribute)
|
||||
|
||||
## Example
|
||||
|
||||
Use TypeScript syntax to create types for TypeBox, Valibot and Zod ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAagIYA2wARhDIQFoQAmcAvnAGZQQhwDkAAgGdgAOwDGZEsCgB6GOgwgSYXgChVMmXAAKJKIIxwAyqhEwSuOAB8deg8XIBXQ+rEQRg+ACU4AXjgMjAAUvJ5QogDm1nAijiCUGFC8AJQAdGB2GCGpOSlw+QVwmnBuHt4AXHBhkdGx8YnqxWiYRmLhYPAmZhau7p7GfnAABgiq+biVdQlQ+GNwqJNx07P5AF6L9VCqLEO9ZSiDzdh4QUbJhReXV9f5xaX9yJXIAPKUAFYYYjAAPKM3-wDAf9iuMngA5JaJFZAmGwm4g+bgyEzOZwtFohHrFAQzao9H4wHFFgAPj2-SIg1IFGoMFO5wJALufXgREqrw+X1aAAtFCRfniGYKLgiJnAcdNubzvjlUsToUKFQUEQsxcjJUppTk5QLFYLMZVxYl1XyZaTdbqiYRTepVPd4HRBoE6eb4Vo7QFKoF2Z8fn8XUKRZ6mIaUf6A1p8irAiH5WH8fqAsHkTq47DLXBTUA))
|
||||
Use TypeScript syntax to create types for TypeBox, Valibot and Zod ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAagIYA2wARhDIQFoQAmcAvnAGZQQhwDkAAgGdgAOwDGZEsCgB6GOgwgSYXgChVMmXADKqETBK4UCwerEQRg+NrgBeOAAMEquHFwAuOCICuIShih8FzhUTx8-AKDXAC8w338oVRYHdU04ACVvfVAMY0xTVXNLeGQ7POw8AAptAEpXeobGpubGtKKrFE9kAHlKACsMMRgAHiQAOgnWAD4zCw6iMtIKahhqupaNzYa2ufgiT16Boe0xAAtFElG4CbHWQhuZwt24OjKGRjWtr++d4pfPd6HQYjcaTFj3CZTIA))
|
||||
|
||||
```typescript
|
||||
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
|
||||
|
||||
// Syntax
|
||||
// Syntax Types
|
||||
|
||||
const S = `{
|
||||
x: number,
|
||||
@@ -85,24 +86,11 @@ const S = `{
|
||||
|
||||
// Runtime Types
|
||||
|
||||
const T = TypeBox(S) // const T: TObject<{
|
||||
// x: TNumber,
|
||||
// y: TNumber,
|
||||
// z: TNumber
|
||||
// }>
|
||||
const T = TypeBox(S) // const T: TObject<{ ... }>
|
||||
|
||||
const V = Valibot(S) // const V: ObjectSchema<{
|
||||
// x: NumberSchema<...>,
|
||||
// y: NumberSchema<...>,
|
||||
// z: NumberSchema<...>
|
||||
// }, ...>
|
||||
const V = Valibot(S) // const V: ObjectSchema<{ ... }, ...>
|
||||
|
||||
|
||||
const Z = Zod(S) // const Z: ZodObject<{
|
||||
// x: ZodNumber,
|
||||
// y: ZodNumber,
|
||||
// z: ZodNumber
|
||||
// }, ...>
|
||||
const Z = Zod(S) // const Z: ZodObject<{ ... }, ...>
|
||||
```
|
||||
|
||||
Translate TypeBox, Valibot and Zod types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAagIYA2wARhDIQFoQAmcAvnAGZQQhwDkAAgGdgAOwDGZEsCgB6GOgwgSYXgChVMmXADKqETBK44AWgB8cBszPFyVGifNpMOXOrEQRg+MjgBeFAouABSkFNQwQZZBAAYIqnBwuABccCIAriCUGFD48XCoKemZ2bkJAF6FGVlQqizRAJSNQA))
|
||||
@@ -119,7 +107,7 @@ const T = TypeBox(Valibot(Zod(`{
|
||||
}`)))
|
||||
```
|
||||
|
||||
Compile Valibot and Zod types on TypeBox validation infrastructure. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIABAZ2ADsBjDAQ2CgHoYBPMLERsUgFADQkWHABehYiDLiIAE0EDWrOAC0FcACpcsQuhBqV4auAF4JAOggAjAFZY6MABRI47j56-efHlXAMjEwAudQUAeXtHGAAeNwF3AA9Q8UsaAFcQGywoZwBKABpfYpLi-yTQjXkAOUzsqCLPBLgOFLS6nPzG0p7S8pbKhVqsnILm8TaMkdzC3rmy1XcJsJqOqAFcPPntnfd-XAA+IX8AJR4+OEYaeRQ0MEw9AUDjFHNb9GxnNS3d3v9n+DIUIANUYGGA8kYMGgMS0kQcTji7mav1RfUWcGS2mG9W6yLRBIWHla2LWY0JFK8-WWWhxORRlIp+wOR2UqgAEsAAOYAC20POI6V5EHS8FB4MhMGAhn0hheJzeyEsyB5jgA1q5GT5-ABJGA5KHSoyhACMAAYAPpm61W63NLEmvFauYqZokgBMTudPX8VV8oQALNazSBKHBnHwwMREnlxqEAMzenb+HTcACyFw8gZDYfcEbAUYgMY2eSAA))
|
||||
Compile Valibot and Zod types on TypeBox infrastructure ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIABAZ2ADsBjDAQ2CgHoYBPMLERsUgFADQkWHABehYiDLiIAE0EDWrOAC0FcACpcsQuhBqV4auAF4JAOggAjAFZY6MABRI47j56-efHlXAMjEwAudQUAeXtHGAAeNwF3AA9Q8UsaAFcQGywoZwBKABpfYpLi-yTQjXkAOUzsqCLPBLgOFLS6nPzG0p7S8pbKhVqsnILm8TaMkdzC3rmy1XcJsJqOqAFcPPntnfd-XAA+IX8AJR4+OEYaeRQ0MEw9AUDjFHNb9GxnNS3d3v9n+DIUIANUYGGA8kYMGgMS0kQcTji7mav1RfUWcGS2mG9W6yLRBIWHla2LWY0JFK8-WWWhxORRlIp+wOR2UqgAEsAAOYAC20POI6V5EHS8FB4MhMGAhn0hheJzeyEsyB5jgA1q5GT5-ABJGA5KHSoyhACMAAYAPpm61W63NLEmvFauYqZokgBMTudPX8VV8oQALNazSBKHBnHwwMREnlxqEAMzenb+HTcACyFw8gZDYfcEbAUYgMY2eSAA))
|
||||
|
||||
```typescript
|
||||
import { Compile } from '@sinclair/typemap'
|
||||
@@ -151,39 +139,47 @@ const R = C.Check({ // Iterations: 10_000_000
|
||||
})
|
||||
```
|
||||
|
||||
<a name="Section-Mapping"></a>
|
||||
|
||||
## Mapping
|
||||
|
||||
TypeMap is designed for runtime type translation. It provides a single mapping functions per library which is used to translate remote types into types specific to that library. All mapping functions make a best attempt to retain semantics from each library. If no translation is possible, these functions return a `never` representation specific to the library being mapped.
|
||||
TypeMap is designed for runtime type translation. It provides one mapping function per library which can be used to translate remote types into types specific to that library. All mapping functions make a best effort attempt to translate the structures and semantics from each library. If no translation is possible, these functions return a `never` representation.
|
||||
|
||||
<a name="Mapping-Syntax"></a>
|
||||
|
||||
### Syntax
|
||||
|
||||
Use the `Syntax` function to translate types into a Syntax string
|
||||
Use the `Syntax` function to translate types into a Syntax string ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYhZgKAKCGhIsOACo4WGnHgkylWvWasOXXgCMIOYWOjxpsuADdipchVNYmwHTH3hDUmXIBeFpRXcQAJsJCDBDoNPDIcAC8KBjYOAAUFGFQ9ADmANoAuhQAlHD5BYVFxQXs7HDBoeEAXJTJaVkUxfFomLg5QSFhcAAqUTFtCTAAdAByAK4gWjxQ8Tl5JYuLZRVd8D21FOiT01BNJfE93DwAQrodld0Aav2tcfGmw-XoqXMLSx+l5ZfwV5vPqX2zSutnsEBgFzWcAAWrdYrh4u5hjoIEweFh0G9PtiVj8YZsUWiMUDCvFof4ckA))
|
||||
|
||||
```typescript
|
||||
import { Syntax } from '@sinclair/typemap'
|
||||
|
||||
const S = Syntax('string[]') // const S: 'string[]' (Syntax)
|
||||
const T = Syntax(t.Number()) // const T: 'number' (TypeBox)
|
||||
const V = Syntax(v.string()) // const V: 'string' (Valibot)
|
||||
const Z = Syntax(z.boolean()) // const Z: 'boolean' (Zod)
|
||||
const S = Syntax('string[]') // const S: 'string[]' (Syntax)
|
||||
const T = Syntax(t.Number()) // const T: 'number' (TypeBox)
|
||||
const V = Syntax(v.string()) // const V: 'string' (Valibot)
|
||||
const Z = Syntax(z.boolean()) // const Z: 'boolean' (Zod)
|
||||
```
|
||||
|
||||
<a name="Mapping-TypeBox"></a>
|
||||
|
||||
### TypeBox
|
||||
|
||||
Use the `TypeBox` function to translate types and syntax into TypeBox types.
|
||||
Use the `TypeBox` function to translate types and syntax into TypeBox types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA84C+cAZlBCHAOQACAzsAHYDGANgIbBQD0M6GIbMJQBQw0JFhwAVHDa048UuSp1GrDt16YARnhHjo8GXLgA3EmQqVTbFsF0x94Q9NnyAXheWV3EACYiwkwQDLTwAMpwALwofDi4ABSUYVCMAOYA2gC6lACUcAWFRcUlBVxccMGhEQBcKACCUFBsqAA8yOEwqQxpAHxwCeGoDDBsuLlBIWEo0bGY8QkwAHQAcgCuINoYUAm5+aUHpeWVU-DIdcjrm9uHCWjzeBNV0wBqs-fYeAmmSynpu-tDkCyhVnvAXhdOt00rcXrZ7BAYE9TnAAFrvOJfdxLXQQFgYNgMAHA4HHMFoi44PEEhgHBKo-y5IA))
|
||||
|
||||
```typescript
|
||||
import { TypeBox } from '@sinclair/typemap'
|
||||
|
||||
const S = TypeBox('string[]') // const S: TArray<TString> (Syntax)
|
||||
const T = TypeBox(t.Number()) // const T: TNumber (TypeBox)
|
||||
const V = TypeBox(v.string()) // const V: TString (Valibot)
|
||||
const Z = TypeBox(z.boolean()) // const Z: TBoolean (Zod)
|
||||
const S = TypeBox('string[]') // const S: TArray<TString> (Syntax)
|
||||
const T = TypeBox(t.Number()) // const T: TNumber (TypeBox)
|
||||
const V = TypeBox(v.string()) // const V: TString (Valibot)
|
||||
const Z = TypeBox(z.boolean()) // const Z: TBoolean (Zod)
|
||||
```
|
||||
|
||||
<a name="Mapping-Valibot"></a>
|
||||
|
||||
### Valibot
|
||||
|
||||
Use the `Valibot` function to translate types and syntax into Valibot types.
|
||||
Use the `Valibot` function to translate types and syntax into Valibot types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgNQIYBtgCMLwL5wBmUEIcA5AAIDOwAdgMbqrBQD0MAnmAKYiphyAKCGhIsOACo4qanHjFSFGvSYt2XXjgAewsdHjTZcAG5ESZciYzZce8Aaky5AL3NLyLiABNhQhhB01PAAynAAvCg2ODAAFOTBUPQA5gDaALrkAJRwuXn5BYW5bGxwAUGhAFymAHQAglBQqJwhDAAWfKgAPDW9AHxwsSGcdDCo2ln+gcFwACoRUZgxsTA1AHIAriBYPFCxWTlFR0UlZdPws9Um61s7UK0d-AWxs9w8AEIQE1MVKAtoS1wsWuiRS+0Ox0hxVK5RmyCuNRCMCSdGSD06zwBthgk1h8AAWv9okCXDUcBB0DxUHRwVCoac8XB8QjPhSqXR0U88rF8T4skA))
|
||||
|
||||
```typescript
|
||||
import { Valibot } from '@sinclair/typemap'
|
||||
@@ -194,9 +190,11 @@ const V = Valibot(v.string()) // const V: v.StringSchema
|
||||
const Z = Valibot(z.boolean()) // const Z: v.BooleanSchema (Zod)
|
||||
```
|
||||
|
||||
<a name="Mapping-Zod"></a>
|
||||
|
||||
### Zod
|
||||
|
||||
Use the `Zod` function to translate types and syntax into Zod types.
|
||||
Use the `Zod` function to translate types and syntax into Zod types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgLQgEzgXzgMyhEOAcgAEBnYAOwGMAbAQ2CgHoYBPMAUxHrCICh+oSLDgAqOPTJx4eAsXJU6jFuy4AjCAA8Bw6PAlS4AN1z5CRY-VrBNMXeH3jJ0gF5n5RV+gH9qESjJ4AGU4AF4UdAAKIiCoKgBzAG0AXSIASjgs7JzcvLzmZjh-QJCALjhXADpUNABBKCh6NgAeKvaAPjgo4LZKGHotdL8AoLgAFXDItCiYKoA5AFcQdU4oKPTM-O2drMLi0fhxiurapZW13Kjxjk4AIW1hkrGANSnaqOMquMSNrd2ATl9s94C8TjV0MEYPFKAkri9rLYIDAnocUO9otVNBBaJx6JQ-oCiXBgWjkODag8cXjKDkorV0kA))
|
||||
|
||||
```typescript
|
||||
import { Zod } from '@sinclair/typemap'
|
||||
@@ -207,61 +205,72 @@ const V = Zod(v.string()) // const V: z.ZodString
|
||||
const Z = Zod(z.boolean()) // const Z: z.ZodBoolean (Zod)
|
||||
```
|
||||
|
||||
<a name="Section-Syntax"></a>
|
||||
|
||||
## Syntax
|
||||
|
||||
TypeMap provides a TypeScript syntax parser that can be used to create library types. TypeScript parsing is implemented at runtime as well as in the TypeScript type system. It offers a convenient means of creating cross library types without having to author across multiple type builder API.
|
||||
TypeMap provides an optional TypeScript syntax parser that can be used to construct library types. Syntax parsing is implemented at runtime as well as in the type system. This feature can be used to uniformly construct types across libraries, as well as create types with embedded remote types.
|
||||
|
||||
<a name="Syntax-Types"></a>
|
||||
|
||||
### Types
|
||||
|
||||
Syntax types can be created by passing a string parameter to any library mapping function. TypeMap supports most TypeScript annotation syntax. If the string contains a syntax error, the function will return a `never` type. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA84C+cAZlBCHAOQACAzsAHYDGANgIbBQD0M6GIbMJQBQwphAa14yOAF4UfHLgAUlJLgBccAIyFKASjhHjJ02aNcuccZOlbkAeQBGAKwxMYAHiTDzf-wGB5pbGmigAMsAwGFBsLJ7aAHy+QalpQSEEyWISUnAAynIKmEqqAIQVBumpITZ5+fYAchgAbjFwALQFqAwwbPgAolBkUEA))
|
||||
Syntax types can be created by passing a string parameter to any library mapping function. TypeMap supports most TypeScript syntax. If the string contains a syntax error, the function will return a `never` type. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA84C+cAZlBCHAOQACAzsAHYDGANgIbBQD0M6GIbMJQBQwphAa14yOAF4UfHLgAUlJLgBccAIwAaOKi0AmQpQCUcS1es3LXLnHGTpW5AHkARgCsMTGAB4kYVsQ0LDwiLh7K00UABlgGAwoNhZ-bQA+XWDI3LzI6MtDeMTk1P8jDJz8mtq7BwIqsQkpOABlOQVMJVUAQn7zOvDop1a21wA5DAA3ZLgAWnbUBhg2fABRKDIoIA))
|
||||
|
||||
```typescript
|
||||
import { TypeBox } from '@sinclair/typemap'
|
||||
|
||||
const T = TypeBox('{ x: 1 }') // const T: TObject<{
|
||||
// x: TLiteral<1>
|
||||
const T = TypeBox('{ x: 1, y: 2 }') // const T: TObject<{
|
||||
// x: TLiteral<1>,
|
||||
// y: TLiteral<2>
|
||||
// }>
|
||||
|
||||
const S = TypeBox('!!!') // const S: TNever - Syntax Error
|
||||
```
|
||||
|
||||
<a name="Syntax-Options"></a>
|
||||
|
||||
### Options
|
||||
|
||||
Options can be passed on the last parameter of a type. TypeMap will translate known Json Schema keywords into appropriate runtime representations if possible. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAWhACZwC+cAZlBCHAOQACAzsAHYDGANgIbAoAehjoMIPmEYAoaUKFwuEDq3jIAXCgDKMKJwDmcALyI4ozJsaq9HfY0LVoEmJfECejSrKUq1xlGI4uAAUVroG9qaOUM6uEsAelACUsvKKyqpwWv4AXgB0EABGAFYYXDDBSLia+RwAriCFGFDBScl51sDlrd4Z8NkmJKShVZr1jc2UkUh8pKTAMMDKfDwACnSYsMAYrJrUK6wYyUA))
|
||||
You can pass Json Schema options on the last parameter of a syntax type. TypeMap translates known [keywords](https://json-schema.org/understanding-json-schema/reference/type) into appropriate runtime representations if possible ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAWhACZwC+cAZlBCHAOQACAzsAHYDGANgIbAoAehjoMIPmEYAoaVwgdW8ZHAC8KMTlwAKRkqicA5o0JI45i5avW4QoXHmLlALhQBlGAY6G1iaeepoCRhXRnEBHkYbaJjY23tzUUxQ-SMTaQoASjic3Ly7C0CoYNDw4Ej-PKrq6wKKWTkFJTg3XxJSXSRcAH5XDgBXEAAjDChKE0Qa8wLHZtb1AC8AOgghgCsMLhhtJEq+UlJgGGAFPh4ABTpMWGAMVldqM9YMKasC81xXZYHh0e1MlZgY6nHj-DLZV6Q2J1AGpLb-IA))
|
||||
|
||||
```typescript
|
||||
import { TypeBox, Zod } from '@sinclair/typemap'
|
||||
|
||||
const T = TypeBox('string', { // const T: TString = {
|
||||
format: 'email' // type: 'string',
|
||||
}) // format: 'email'
|
||||
// }
|
||||
const T = TypeBox('string', { // const T: TString = {
|
||||
format: 'email' // type: 'string',
|
||||
}) // format: 'email'
|
||||
// }
|
||||
|
||||
|
||||
const S = Zod('{ x: number }', { // const S = z.object({
|
||||
additionalProperties: false // x: z.number()
|
||||
const S = Zod('{ x?: number }', { // const S = z.object({
|
||||
additionalProperties: false // x: z.number().optional()
|
||||
}) // }).strict()
|
||||
```
|
||||
|
||||
<a name="Syntax-Parameters"></a>
|
||||
|
||||
### Parameters
|
||||
|
||||
Types can be parameterized to accept exterior types. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgNQIYBtgCMIwDRwBaEAJnAL5wBmUEIcA5AAIDOwAdgMbqrBQD0MAJ5gApiFRgGAKGmcI7FvAAqcALwoM2XAAoG7AK4gsoqAwCUcK9f784AUQAeMU8GhxlI0bPmL4AZXUiUh0kVXICAAMkRwAuDwpIy1tgkgB5LAArUU4YAB4Y+OISADkjEygKAiQAOjqKAD4gA))
|
||||
Syntax types can be parameterized to receive exterior types. Parameters are automatically translated to the receiving type ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgNQIYBtgCMIwDRwBaEAJnAL5wBmUEIcA5AAIDOwAdgMbqrBQD0MAJ5gApiFRgGAKGmcI7FvGRwAvCgzZcACgbsAriCyioDAJRxLV6zdvX+-OPMXKAXHAByh41ADKnAAtxVFkHOAAFVChUEFEYExQ4CTAxMipoOBgogHM4ollnJSI1IlJtJBVyAgADJAA3DH1RFndkAG0AXQpqizsbMML4QndiEgB5LAArUU4YAB4kaT7lldW1yzDLBvQmltKSAEEoaKE50a8jEwA+JfW7++WwqsQ4ADp3iiugA))
|
||||
|
||||
```typescript
|
||||
import { Valibot, Zod } from '@sinclair/typemap'
|
||||
|
||||
const T = Valibot('number') // const T: NumberSchema
|
||||
const V = Valibot('number') // const V: NumberSchema
|
||||
|
||||
// Parameter T auto remapped to target library
|
||||
// Parameter V mapped for target Z
|
||||
|
||||
const S = Zod({ T }, `{ x: T }`) // const S: ZodObject<{
|
||||
// x: ZodNumber
|
||||
const Z = Zod({ V }, `{ values: V[] }`) // const Z: ZodObject<{
|
||||
// values: ZodArray<ZodNumber>
|
||||
// }, { ... }>
|
||||
```
|
||||
|
||||
<a name="Syntax-Generics"></a>
|
||||
|
||||
### Generics
|
||||
|
||||
Use parameterized types with functions to create generic types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAagIYA2wARhDIQFoQAmcAvnAGZQQhwDkAAgGdgAOwDGZEsCgB6GOgwgSYXgChVMmXADiGERijAxKBerEQRg+EQxiY0OAF44AHmRwMuGHsaC4ESgArWxgAPgAKZAAuFABKJ1CTTBxccKR3FkIAAyRVODhcGORCPLhUIpL8gC8iuFUWLNj1TTgASUsYEnEMZjRMQTMLKxQnYhDocNIKahhw3hEAVxBKA15Y+PzNre38lvMOlCKAeSCQlwRSnavrm9vblvzClAA5JZWoSruv75+4B7Kiq9lgZ8JdfuCIX8tNVAW8QWDIYi7i0WKFBgcAMqjGx2CYMRhzKyGEQAczWG0ReyG8Axx1OdnOCKRzJ2-yeyAxMGJJM+LL523+5RQnO5oP54s2-xqwq5oh5TIlSJRoSAA))
|
||||
Use parameterized types with functions to create generic syntax types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAagIYA2wARhDIQFoQAmcAvnAGZQQhwDkAAgGdgAOwDGZEsCgB6GOgwgSYXgChVMmXADiGERijAxKBerEQRg+EQxiY0OAF44AHmRwMuGHsaC4ESgArWxgAPgAKZAAuFABKJ1CTTBxccKR3FkIAAyRVODhcGORCPLhUIpL8gC8iuFUWLNj1TTgASUsYEnEMZjRMQTMLKxQnYhDocNIKahhw3hEAVxBKA15Y+PzNre38lvMOlCKAeSCQlwRSnavrm9vblvzClAA5JZWoSruv75+4B7Kiq9lgZ8JdfuCIX8tNVAW8QWDIYi7i0WKFBgcAMqjGx2CYMRhzKyGEQAczWG0ReyG8Axx1OdnOCKRzJ2-yeyAxMGJJM+LL523+5RQnO5oP54s2-xqwq5oh5TIlSJRoSAA))
|
||||
|
||||
```typescript
|
||||
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
|
||||
@@ -307,12 +316,43 @@ type V = Static<typeof V> // string
|
||||
type Z = Static<typeof Z> // boolean
|
||||
```
|
||||
|
||||
## TreeShake
|
||||
<a name="Json-Schema"></a>
|
||||
|
||||
TypeMap takes TypeBox, Valibot and Zod on as peer dependencies. If bundling for browser environments, it is recommended to import specific functions to handle specific translations. For example, the following imports a Zod to TypeBox translation. By doing this it will by-pass other library imports cause Valibot to be omitted from the bundle.
|
||||
## Json Schema
|
||||
|
||||
Use `TypeBox` to transform remote library types into Json Schema ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA84CGAznAFJEQB2AygMYAWGIBcAvnAGZQQhwDkAASLBKtADYFgUAPQx0TAmD4AoZaEiw4AKkIkAXp268+eiABMVy2lSLwAWnAC8cPQDoIAIwBWGWjAAUSHDBIaFh4REh0tJw1pS2cHYAXC6uduYA8t6+MAA8CMrBuClulACuIB4YUP4AlAA0kU3NTdEhxanpZgByFVVQANyFcKglruWV1XWNLbOzbcGjnea9k4PDemMT-XVze80LLmNdq-1DrLWutlDAfrv7D60xrI0ARNe3MK+Nrr8AfKo2mhMDh8BhROYMCQ5JgSMQyBRKHA6IxmKo4glkE4EVQUQp-HZao95jEMfAsc4CsTqftDjCMCk+J4fH4+PVhjTOU8QlAMABHMpSDBmFIAbT4uDZ-FQUpMfAAuuyucqIocCGYzMAYMAqAQxAAFbiYWDAKEpDh6ogYJUq23BQ5gI3VbVmxAcu3Kw5FFJIemM7bVPhsG0ez0xUJLX3yf19QPB92hmleo6IOB+-gBqBB1gJxPEw45vNctqsIA))
|
||||
|
||||
```typescript
|
||||
import { TypeBoxFromZod } from '@sinclair/typemap' // Include TypeBox & Zod, Tree Shake Valibot
|
||||
import { TypeBox as JsonSchema } from '@sinclair/typemap'
|
||||
|
||||
const Z = z.object({ // const Z: z.ZodObject<{
|
||||
x: z.number(), // x: z.ZodNumber;
|
||||
y: z.number(), // y: z.ZodNumber;
|
||||
z: z.number() // z: z.ZodNumber;
|
||||
}).strict() // }, "strict", ...>
|
||||
|
||||
// TypeBox encodes types as Json Schema
|
||||
|
||||
const T = JsonSchema(Z) // const T = {
|
||||
// type: 'object',
|
||||
// required: ['x', 'y', 'z'],
|
||||
// additionalProperties: false,
|
||||
// properties: {
|
||||
// x: { type: 'number' },
|
||||
// y: { type: 'number' },
|
||||
// z: { type: 'number' }
|
||||
// }
|
||||
// }
|
||||
```
|
||||
|
||||
<a name="Tree-Shake"></a>
|
||||
|
||||
## Tree Shake
|
||||
|
||||
TypeMap takes on TypeBox, Valibot and Zod as peer dependencies. If bundling, it is recommended that you import specific translation functions. By doing this it will by-pass library imports and enable unused libraries to be omitted from the bundle.
|
||||
|
||||
```typescript
|
||||
import { TypeBoxFromZod } from '@sinclair/typemap' // Use TypeBox & Zod, Tree Shake Valibot
|
||||
|
||||
import * as z from 'zod'
|
||||
|
||||
@@ -325,12 +365,12 @@ const T = TypeBoxFromZod(z.object({ // const T: TObject<{
|
||||
|
||||
## Compile
|
||||
|
||||
Use the `Compile` function to compile Zod and Valibot on TypeBox validation infrastructure. ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApgGjgLQgBM4BfOAMyjTgHIABAZ2ADsBjDAQ2CgHoYAnmCwhOYWgCgJvXijRhMWOABUhWKWwgtG8AGpwAvHPTYAFISKmABgglx7ADwBccFgFcQAIyxQcd+wIu7l4+fvZwAF5BHt5QEqRWAJSJUjIqagCyYi7IABZYbADWcABibuwwwFpwpiWcOikSmtrwAEoAjIZwugB0eQWFpkjOcO14gXAATHhRcADMZI1pAMownCxEnFAky2z5oi66nBjAmzBKZRVVLDV1DRpaOnCtk126ANq0AH4665vbtAAuj0AG7HU6cc5DOAjMZwCbTSIuBakRJAA))
|
||||
Use the `Compile` function to compile TypeBox, Valibot and Zod on TypeBox infrastructure ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgYQuYAbApgGjgLQgBM4BfOAMyjTgHIABAZ2ADsBjDAQ2CgHoYAnmCwhOYWgCgJvXijRhMWAsTgAVIVilsILRvABunDMCKcY0OAF456bAApCROwAMEEuB4AeALjgsAriAARlhQOO4eAr4BwaHhHnAAXtGBIVASpM4AlFlaOnpwAEoAjFZwhsam5lAAdMgAFlhsANZ2SD5wxXhRcABMeMlwAMxkWQlwMnAAgmxsWNhQZlhEUpMAyjCcLKZQJGtsjaLSvMdwNedwAO71wAdwjPUQ-hgk9Zz6SiFYLH6cIMt0DZbHYkABqRhMZmgkgk2l08EKvTKFUh1QA2rQAH56YGcXa0AC6NRRVSwbTgHS6cB6-SSvhGpCyQA))
|
||||
|
||||
```typescript
|
||||
import { Compile, Zod } from '@sinclair/typemap'
|
||||
|
||||
// Compile Validator From Zod
|
||||
// Compile Zod Type
|
||||
|
||||
const validator = Compile(Zod(`{
|
||||
x: number,
|
||||
@@ -338,7 +378,7 @@ const validator = Compile(Zod(`{
|
||||
z: number
|
||||
}`))
|
||||
|
||||
const R1 = validator.Check({ x: 1, y: 2, z: 3 })
|
||||
const R1 = validator.Check({ x: 1, y: 2, z: 3 }) // Accelerated
|
||||
|
||||
// Standard Schema
|
||||
//
|
||||
|
||||
BIN
typemap.png
BIN
typemap.png
Binary file not shown.
|
Before Width: | Height: | Size: 696 KiB After Width: | Height: | Size: 700 KiB |
Reference in New Issue
Block a user