mirror of
https://github.com/zoriya/typebox.git
synced 2026-05-27 00:26:52 +00:00
typebox-v3
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
TypeBox: JSONSchema Type Builder with Static Type Resolution for TypeScript
|
||||
|
||||
The MIT License (MIT)
|
||||
@@ -22,6 +20,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
THE SOFTWARE.
|
||||
@@ -9,26 +9,25 @@
|
||||
|
||||
<img src='./doc/example.gif'></img>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<a name="Install"></a>
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
npm install @sinclair/typebox --save
|
||||
```bash
|
||||
$ npm install @sinclair/typebox --save
|
||||
```
|
||||
|
||||
<a name="Overview"></a>
|
||||
|
||||
## Overview
|
||||
|
||||
TypeBox is a type builder library that allows developers to compose complex in-memory JSONSchema objects that can be resolved to static TypeScript types. TypeBox internally represents its types as plain JSONSchema objects and leverages TypeScript's [Mapped Types](https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types) to infer schemas to equivalent static type representations. No additional build process is required.
|
||||
TypeBox is a type builder library that allows developers to compose complex in-memory JSONSchema objects that can be resolved to static TypeScript types. The schemas produced by TypeBox can be used directly as validation schemas or reflected upon by navigating the standard JSONSchema properties at runtime.
|
||||
|
||||
TypeBox can be used as a tool to build and validate complex schemas, or integrated into RPC or REST services to help validate data received over the wire or published directly to consumers to service as developer documentation.
|
||||
TypeBox can be used as a simple tool to build complex schemas or integrated into RPC or REST services to help validate JSON data received over the wire.
|
||||
|
||||
Note that TypeBox does not provide any mechanisms for validating JSONSchema. Please refer to libraries such as [AJV](https://www.npmjs.com/package/ajv) or similar to validate the schemas created with this library.
|
||||
TypeBox does not provide any mechanism for validating JSONSchema. Please refer to libraries such as [AJV](https://www.npmjs.com/package/ajv) or similar to validate the schemas created with this library.
|
||||
|
||||
Requires TypeScript 3.8.3 and above.
|
||||
|
||||
@@ -39,13 +38,14 @@ License MIT
|
||||
- [Overview](#Overview)
|
||||
- [Example](#Example)
|
||||
- [Types](#Types)
|
||||
- [Other Types](#Intrinsics)
|
||||
- [More Types](#Intrinsics)
|
||||
- [Functions](#Functions)
|
||||
- [Generics](#Generics)
|
||||
- [Validation](#Validation)
|
||||
|
||||
## Example
|
||||
|
||||
The following shows the type alias for `Order` and its TypeBox equivalent.
|
||||
The following shows the general usage.
|
||||
|
||||
```typescript
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
@@ -96,12 +96,10 @@ JSON.validate(Order, { // IETF | TC39 ?
|
||||
|
||||
## Types
|
||||
|
||||
TypeBox functions generate JSONschema objects. The following table outlines the TypeScript and JSONSchema equivalence.
|
||||
TypeBox provides many functions generate JSONschema data types. The following tables list the functions TypeBox provides and their respective TypeScript and JSONSchema equivalents.
|
||||
|
||||
### TypeBox > TypeScript
|
||||
|
||||
The following types and modifiers are compatible with JSONschema and have both JSONschema and TypeScript representations.
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -206,8 +204,6 @@ The following types and modifiers are compatible with JSONschema and have both J
|
||||
|
||||
### TypeBox > JSONSchema
|
||||
|
||||
The following shows the TypeBox to JSONSchema mappings. The following schemas are returned from each function.
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -264,7 +260,7 @@ The following shows the TypeBox to JSONSchema mappings. The following schemas ar
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Tuple</td>
|
||||
<td><code>const T = Type.Union(Type.Number(), Type.String())</code></td>
|
||||
<td><code>const T = Type.Tuple(Type.Number(), Type.String())</code></td>
|
||||
<td><code>{ type: "array", items: [{type: 'string'}, {type: 'number'}], additionalItems: false, minItems: 2, maxItems: 2 }</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -303,13 +299,13 @@ The following shows the TypeBox to JSONSchema mappings. The following schemas ar
|
||||
|
||||
<a name="Intrinsics"></a>
|
||||
|
||||
## Other Types
|
||||
## More Types
|
||||
|
||||
TypeBox provides some non-standard JSONSchema functions that TypeBox refers to as Intrinsic types. While these types cannot be used with JSONSchema, they do provide similar reflection and introspection metadata for expressing function signatures with TypeBox.
|
||||
In addition to the JSONSchema functions, TypeBox also provides some non-standard schemas that provide reflectable metadata for function signatures. These functions allow TypeBox to express `function` and `constructor` signatures where the arguments and return types may be JSONSchema.
|
||||
|
||||
See [Functions](#Functions) section for more details.
|
||||
For more information on their use, see the [Functions](#Functions) and [Generics](#Generics) sections below.
|
||||
|
||||
### TypeBox > Intrinsics
|
||||
### TypeBox > TypeScript
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
@@ -324,7 +320,12 @@ TypeBox provides some non-standard JSONSchema functions that TypeBox refers to a
|
||||
<td>Function</td>
|
||||
<td><code>const T = Type.Function([Type.String()], Type.String())</code></td>
|
||||
<td><code>type T = (arg0: string) => string</code></td>
|
||||
</tr>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Constructor</td>
|
||||
<td><code>const T = Type.Constructor([Type.String()], Type.String())</code></td>
|
||||
<td><code>type T = new (arg0: string) => string</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Promise</td>
|
||||
<td><code>const T = Type.Promise(Type.String())</code></td>
|
||||
@@ -343,7 +344,7 @@ TypeBox provides some non-standard JSONSchema functions that TypeBox refers to a
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
### TypeBox > Non Schema
|
||||
### TypeBox > TypeBox Schema
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
@@ -358,7 +359,12 @@ TypeBox provides some non-standard JSONSchema functions that TypeBox refers to a
|
||||
<td>Function</td>
|
||||
<td><code>const T = Type.Function([Type.String()], Type.Number())</code></td>
|
||||
<td><code>{ type: 'function', arguments: [ { type: 'string' } ], returns: { type: 'number' } }</code></td>
|
||||
</tr>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Constructor</td>
|
||||
<td><code>const T = Type.Constructor([Type.String()], Type.Number())</code></td>
|
||||
<td><code>{ type: 'constructor', arguments: [ { type: 'string' } ], returns: { type: 'number' } }</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Promise</td>
|
||||
<td><code>const T = Type.Promise(Type.String())</code></td>
|
||||
@@ -381,13 +387,12 @@ TypeBox provides some non-standard JSONSchema functions that TypeBox refers to a
|
||||
|
||||
## Functions
|
||||
|
||||
TypeBox provides some capabilities for building typed function signatures. It is important to note however that unlike the other functions available on `Type` the `Type.Function(...)` and other intrinsic types do not produce valid JSONSchema. However, the types returned from `Type.Function(...)` may be comprised of schemas that describe its `arguments` and `return` types. Consider the following TypeScript and TypeBox variants.
|
||||
The following demonstrates creating function signatures for the following TypeScript types.
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
|
||||
// TypeScript
|
||||
|
||||
type T0 = (a0: number, a0: number) => number;
|
||||
type T0 = (a0: number, a1: string) => boolean;
|
||||
|
||||
type T1 = (a0: string, a1: () => string) => void;
|
||||
|
||||
@@ -395,28 +400,106 @@ type T2 = (a0: string) => Promise<number>;
|
||||
|
||||
type T3 = () => () => string;
|
||||
|
||||
// Convention
|
||||
type T4 = new () => string
|
||||
```
|
||||
|
||||
Type.Function([...Arguments], ReturnType)
|
||||
### TypeBox
|
||||
|
||||
// TypeBox
|
||||
|
||||
const T0 = Type.Function([Type.Number(), Type.Number()], Type.Number())
|
||||
```typescript
|
||||
const T0 = Type.Function([Type.Number(), Type.String()], Type.Boolean())
|
||||
|
||||
const T1 = Type.Function([Type.String(), Type.Function([], Type.String())], Type.Void())
|
||||
|
||||
const T2 = Type.Function([Type.String()], Type.Promise(Type.Number()))
|
||||
|
||||
const T3 = Type.Function([], Type.Function([], Type.String()))
|
||||
|
||||
const T4 = Type.Constructor([], Type.String())
|
||||
```
|
||||
|
||||
<a name="Generics"></a>
|
||||
|
||||
## Generics
|
||||
|
||||
Generic function signatures can be composed with TypeScript functions with [Generic Constraints](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints).
|
||||
|
||||
### TypeScript
|
||||
```typescript
|
||||
type ToString = <T>(t: T) => string
|
||||
```
|
||||
### TypeBox
|
||||
```typescript
|
||||
import { Type, Static, TStatic } from '@sinclair/typebox'
|
||||
|
||||
const ToString = <G extends TStatic>(T: G) => Type.Function([T], Type.String())
|
||||
```
|
||||
However, it's not possible to statically infer what type `ToString` is without first creating some specialized variant of it. The following creates a specialization called `NumberToString`.
|
||||
```typescript
|
||||
const NumberToString = ToString(Type.Number())
|
||||
|
||||
type X = Static<typeof NumberToString>
|
||||
|
||||
// X is (arg0: number) => string
|
||||
```
|
||||
To take things a bit further, the following code contains some generic TypeScript REST setup with controllers that take some generic resource of type `T`. Below this we expresses that same setup using TypeBox. The resulting type `IRecordController` contains reflectable metadata about the `RecordController`.
|
||||
### TypeScript
|
||||
```typescript
|
||||
interface IController<T> {
|
||||
get (): Promise<T>
|
||||
post (resource: T): Promise<void>
|
||||
put (resource: T): Promise<void>
|
||||
delete (resource: T): Promise<void>
|
||||
}
|
||||
|
||||
interface Record {
|
||||
key: string
|
||||
value: string
|
||||
}
|
||||
|
||||
class StringController implements IController<Record> {
|
||||
async get (): Promise<Record> { throw 'not implemented' }
|
||||
async post (resource: Record): Promise<void> { /* */ }
|
||||
async put (resource: Record): Promise<void> { /* */ }
|
||||
async delete(resource: Record): Promise<void> { /* */ }
|
||||
}
|
||||
```
|
||||
|
||||
### TypeBox
|
||||
|
||||
```typescript
|
||||
import { Type, Static, TStatic } from '@sinclair/typebox'
|
||||
|
||||
const IController = <G extends TStatic>(T: G) => Type.Object({
|
||||
get: Type.Function([], Type.Promise(T)),
|
||||
post: Type.Function([T], Type.Promise(Type.Void())),
|
||||
put: Type.Function([T], Type.Promise(Type.Void())),
|
||||
delete: Type.Function([T], Type.Promise(Type.Void())),
|
||||
})
|
||||
|
||||
type Record = Static<typeof Record>
|
||||
const Record = Type.Object({
|
||||
key: Type.String(),
|
||||
value: Type.String()
|
||||
})
|
||||
|
||||
type IRecordController = Static<typeof IRecordController>
|
||||
const IRecordController = IController(Record)
|
||||
|
||||
class RecordController implements IRecordController {
|
||||
async get (): Promise<Record> { throw 'not implemented' }
|
||||
async post (resource: Record): Promise<void> { /* */ }
|
||||
async put (resource: Record): Promise<void> { /* */ }
|
||||
async delete(resource: Record): Promise<void> { /* */ }
|
||||
}
|
||||
|
||||
// Reflect !!
|
||||
console.log(IRecordController)
|
||||
```
|
||||
<a href='Validation'></a>
|
||||
|
||||
## Validation
|
||||
|
||||
TypeBox does not provide any mechanism for validating JSONSchema out of the box. Users are expected to bring their own JSONSchema validation library. The following demonstrates how you might enable validation with the AJV npm module.
|
||||
|
||||
### General
|
||||
The following uses the library [Ajv](https://www.npmjs.com/package/ajv) to validate a type.
|
||||
|
||||
```typescript
|
||||
import * Ajv from 'ajv'
|
||||
@@ -427,49 +510,3 @@ ajv.validate(Type.String(), 'hello') // true
|
||||
|
||||
ajv.validate(Type.String(), 123) // false
|
||||
```
|
||||
|
||||
### Runtime Type Validation
|
||||
|
||||
The following demonstrates how you might want to approach runtime type validation with TypeBox. The following
|
||||
code creates a function that takes a signature type `S` which is used to infer function arguments. The body
|
||||
of the function validates with the signatures `arguments` and `returns` schemas against values passed by the
|
||||
caller.
|
||||
|
||||
```typescript
|
||||
import { Type, Static, TFunction } from '@sinclair/typebox'
|
||||
|
||||
// Some validation function.
|
||||
declare function validate(schema: any, data: any): boolean;
|
||||
|
||||
// A function that returns a closure that validates its
|
||||
// arguments and return value from the given signature.
|
||||
function Func<S extends TFunction>(signature: S, func: Static<S>): Static<S> {
|
||||
const validator = (...params: any[]) => {
|
||||
params.forEach((param, index) => {
|
||||
if(!validate(signature.arguments[index], param)) {
|
||||
console.log('error on argument', index)
|
||||
}
|
||||
})
|
||||
const result = (func as Function)(...params);
|
||||
if(!validate(signature.return, result)) {
|
||||
console.log('error on return')
|
||||
}
|
||||
return result
|
||||
}
|
||||
return validator as Static<S>
|
||||
}
|
||||
|
||||
// Create some function.
|
||||
const Add = Func(
|
||||
Type.Function([
|
||||
Type.Number(),
|
||||
Type.Number()
|
||||
], Type.Number()),
|
||||
(a, b) => {
|
||||
return a + b
|
||||
})
|
||||
|
||||
// Call it
|
||||
Add(20, 30)
|
||||
|
||||
```
|
||||
+88
-35
@@ -37,14 +37,14 @@ function reflect(value: any): 'string' | 'number' | 'boolean' | 'unknown' {
|
||||
|
||||
// #region TIntrinsics
|
||||
|
||||
interface TFunction8<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, T7 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5, T6, T7], return: U }
|
||||
interface TFunction7<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5, T6], return: U }
|
||||
interface TFunction6<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5], return: U }
|
||||
interface TFunction5<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4], return: U }
|
||||
interface TFunction4<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3], return: U }
|
||||
interface TFunction3<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2], return: U }
|
||||
interface TFunction2<T0 extends TSchema, T1 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1], return: U }
|
||||
interface TFunction1<T0 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0], return: U }
|
||||
interface TFunction8<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, T7 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5, T6, T7], returns: U }
|
||||
interface TFunction7<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5, T6], returns: U }
|
||||
interface TFunction6<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4, T5], returns: U }
|
||||
interface TFunction5<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3, T4], returns: U }
|
||||
interface TFunction4<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2, T3], returns: U }
|
||||
interface TFunction3<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1, T2], returns: U }
|
||||
interface TFunction2<T0 extends TSchema, T1 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0, T1], returns: U }
|
||||
interface TFunction1<T0 extends TSchema, U extends TSchema> { type: 'function', arguments: [T0], returns: U }
|
||||
interface TFunction0<U extends TSchema> { type: 'function', arguments: [], returns: U }
|
||||
export type TFunction = TFunction8<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TFunction7<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
@@ -56,7 +56,26 @@ export type TFunction = TFunction8<TSchema, TSchema, TSchema, TSchema, TSchema,
|
||||
TFunction1<TSchema, TSchema> |
|
||||
TFunction0<TSchema>
|
||||
|
||||
export type TIntrinsic = TFunction | TVoid | TUndefined | TPromise<any>
|
||||
interface TConstructor8<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, T7 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2, T3, T4, T5, T6, T7], returns: U }
|
||||
interface TConstructor7<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2, T3, T4, T5, T6], returns: U }
|
||||
interface TConstructor6<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2, T3, T4, T5], returns: U }
|
||||
interface TConstructor5<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2, T3, T4], returns: U }
|
||||
interface TConstructor4<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2, T3], returns: U }
|
||||
interface TConstructor3<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1, T2], returns: U }
|
||||
interface TConstructor2<T0 extends TSchema, T1 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0, T1], returns: U }
|
||||
interface TConstructor1<T0 extends TSchema, U extends TSchema> { type: 'constructor', arguments: [T0], returns: U }
|
||||
interface TConstructor0<U extends TSchema> { type: 'constructor', arguments: [], returns: U }
|
||||
export type TConstructor = TConstructor8<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor7<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor6<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor5<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor4<TSchema, TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor3<TSchema, TSchema, TSchema, TSchema> |
|
||||
TConstructor2<TSchema, TSchema, TSchema> |
|
||||
TConstructor1<TSchema, TSchema> |
|
||||
TConstructor0<TSchema>
|
||||
|
||||
export type TIntrinsic = TFunction | TConstructor | TPromise<any> | TVoid | TUndefined
|
||||
export interface TPromise<T extends TSchema | TVoid | TUndefined> { type: 'promise', item: T }
|
||||
export interface TVoid { type: 'void' }
|
||||
export interface TUndefined { type: 'undefined' }
|
||||
@@ -123,8 +142,8 @@ export type TComposite = TIntersect | TUnion | TTuple
|
||||
|
||||
// #region TModifier
|
||||
|
||||
export type TOptional<T extends TSchema | TUnion | TIntersect | TTuple> = T & { modifier: 'optional' }
|
||||
export type TReadonly<T extends TSchema | TUnion | TIntersect | TTuple> = T & { modifier: 'readonly' }
|
||||
export type TOptional<T extends TSchema | TComposite> = T & { modifier: 'optional' }
|
||||
export type TReadonly<T extends TSchema | TComposite> = T & { modifier: 'readonly' }
|
||||
export type TModifier = TOptional<any> | TReadonly<any>
|
||||
|
||||
// #endregion
|
||||
@@ -141,10 +160,10 @@ export type TLiteral = TStringLiteral<string> | TNumberLiteral<number> | TBoolea
|
||||
export interface TStringLiteral<T> { type: 'string', enum: [T] }
|
||||
export interface TNumberLiteral<T> { type: 'number', enum: [T] }
|
||||
export interface TBooleanLiteral<T> { type: 'boolean', enum: [T] }
|
||||
export interface TProperties { [key: string]: TSchema | TUnion | TIntersect | TTuple | TOptional<TSchema | TUnion | TIntersect | TTuple> | TReadonly<TSchema | TUnion | TIntersect | TTuple> }
|
||||
export interface TProperties { [key: string]: TSchema | TComposite | TOptional<TSchema | TComposite> | TReadonly<TSchema | TComposite> }
|
||||
export interface TObject<T extends TProperties> { type: 'object', properties: T, required: string[] }
|
||||
export interface TMap <T extends TSchema | TUnion | TIntersect | TTuple> { type: 'object', additionalProperties: T }
|
||||
export interface TArray <T extends TSchema | TUnion | TIntersect | TTuple> { type: 'array', items: T }
|
||||
export interface TMap <T extends TSchema | TComposite> { type: 'object', additionalProperties: T }
|
||||
export interface TArray <T extends TSchema | TComposite> { type: 'array', items: T }
|
||||
export interface TNumber { type: 'number' }
|
||||
export interface TString { type: 'string' }
|
||||
export interface TBoolean { type: 'boolean' }
|
||||
@@ -172,8 +191,22 @@ type StaticFunction<T> =
|
||||
T extends TFunction0<infer R> ? () => Static<R> :
|
||||
never;
|
||||
|
||||
|
||||
type StaticConstructor<T> =
|
||||
T extends TConstructor8<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer U7, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>, arg7: Static<U7>) => Static<R> :
|
||||
T extends TConstructor7<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>) => Static<R> :
|
||||
T extends TConstructor6<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>) => Static<R> :
|
||||
T extends TConstructor5<infer U0, infer U1, infer U2, infer U3, infer U4, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>) => Static<R> :
|
||||
T extends TConstructor4<infer U0, infer U1, infer U2, infer U3, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>) => Static<R> :
|
||||
T extends TConstructor3<infer U0, infer U1, infer U2, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>) => Static<R> :
|
||||
T extends TConstructor2<infer U0, infer U1, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>) => Static<R> :
|
||||
T extends TConstructor1<infer U0, infer R> ? new (arg0: Static<U0>) => Static<R> :
|
||||
T extends TConstructor0<infer R> ? new () => Static<R> :
|
||||
never;
|
||||
|
||||
type StaticInstrinsic<T extends TIntrinsic> =
|
||||
T extends TFunction ? StaticFunction<T> :
|
||||
T extends TFunction ? StaticFunction<T> :
|
||||
T extends TConstructor ? StaticConstructor<T> :
|
||||
T extends TPromise<infer U> ? Promise<Static<U>> :
|
||||
T extends TVoid ? void :
|
||||
T extends TUndefined ? undefined :
|
||||
@@ -333,25 +366,6 @@ export class Type {
|
||||
return { }
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region PrimitiveExtended
|
||||
|
||||
/** Creates a Promise type. */
|
||||
public static Promise<T extends TSchema>(t: T): TPromise<T> {
|
||||
return { type: 'promise', item: t }
|
||||
}
|
||||
|
||||
/** Creates a Void type. */
|
||||
public static Void(): TVoid {
|
||||
return { type: 'void' }
|
||||
}
|
||||
|
||||
/** Creates a Undefined type. */
|
||||
public static Undefined(): TUndefined {
|
||||
return { type: 'undefined' }
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Literal
|
||||
@@ -448,7 +462,8 @@ export class Type {
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region TFunction
|
||||
// #region TIntrinsic
|
||||
|
||||
/** Creates a Function type for the given arguments. */
|
||||
public static Function<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, T4 extends TStatic, T5 extends TStatic, T6 extends TStatic, T7 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3, T4, T5, T6, T7], returns: U): TFunction8<T0, T1, T2, T3, T4, T5, T6, T7, U>
|
||||
/** Creates a Function type for the given arguments. */
|
||||
@@ -471,7 +486,45 @@ export class Type {
|
||||
public static Function(args: TStatic[], returns: TStatic): TFunction {
|
||||
return { type: 'function', arguments: args, returns: returns } as TFunction
|
||||
}
|
||||
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, T4 extends TStatic, T5 extends TStatic, T6 extends TStatic, T7 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3, T4, T5, T6, T7], returns: U): TConstructor8<T0, T1, T2, T3, T4, T5, T6, T7, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, T4 extends TStatic, T5 extends TStatic, T6 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3, T4, T5, T6], returns: U): TConstructor7<T0, T1, T2, T3, T4, T5, T6, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, T4 extends TStatic, T5 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3, T4, T5], returns: U): TConstructor6<T0, T1, T2, T3, T4, T5, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, T4 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3, T4], returns: U): TConstructor5<T0, T1, T2, T3, T4, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, T3 extends TStatic, U extends TStatic>(args: [T0, T1, T2, T3], returns: U): TConstructor4<T0, T1, T2, T3, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, T2 extends TStatic, U extends TStatic>(args: [T0, T1, T2], returns: U): TConstructor3<T0, T1, T2, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, T1 extends TStatic, U extends TStatic>(args: [T0, T1], returns: U): TConstructor2<T0, T1, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<T0 extends TStatic, U extends TStatic>(args: [T0], returns: U): TConstructor1<T0, U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor<U extends TStatic>(args: [], returns: U): TConstructor0<U>
|
||||
/** Creates a Constructor type for the given arguments. */
|
||||
public static Constructor(args: TStatic[], returns: TStatic): TConstructor {
|
||||
return { type: 'constructor', arguments: args, returns: returns } as TConstructor
|
||||
}
|
||||
|
||||
/** Creates a Promise type. */
|
||||
public static Promise<T extends TSchema>(t: T): TPromise<T> {
|
||||
return { type: 'promise', item: t }
|
||||
}
|
||||
|
||||
/** Creates a Void type. */
|
||||
public static Void(): TVoid {
|
||||
return { type: 'void' }
|
||||
}
|
||||
|
||||
/** Creates a Undefined type. */
|
||||
public static Undefined(): TUndefined {
|
||||
return { type: 'undefined' }
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Extended
|
||||
|
||||
@@ -17,7 +17,7 @@ export async function build() {
|
||||
await shell('tsc -p ./src/tsconfig.json --outDir dist').exec()
|
||||
await folder('dist').add('package.json').exec()
|
||||
await folder('dist').add('readme.md').exec()
|
||||
await folder('dist').add('license.md').exec()
|
||||
await folder('dist').add('license').exec()
|
||||
await shell('cd dist && npm pack').exec()
|
||||
|
||||
// npm publish sinclair-typebox-0.x.x.tgz --access=public
|
||||
|
||||
Reference in New Issue
Block a user