@nmtjs/type 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler.js +16 -79
- package/dist/compiler.js.map +1 -1
- package/dist/index.js +30 -54
- package/dist/index.js.map +1 -1
- package/dist/inference.js +1 -0
- package/dist/inference.js.map +1 -0
- package/dist/parse.js +145 -0
- package/dist/parse.js.map +1 -0
- package/dist/runtime.js +73 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schemas/default.js +6 -0
- package/dist/schemas/default.js.map +1 -0
- package/dist/schemas/discriminated-union.js.map +1 -1
- package/dist/schemas/nullable.js.map +1 -1
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +0 -1
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +0 -1
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +4 -13
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +0 -1
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +0 -1
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +9 -1
- package/dist/types/date.js.map +1 -1
- package/dist/types/enum.js +6 -4
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +3 -2
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +0 -1
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +2 -3
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +31 -1
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +0 -1
- package/dist/types/string.js.map +1 -1
- package/dist/types/union.js +8 -7
- package/dist/types/union.js.map +1 -1
- package/package.json +8 -8
- package/src/compiler.ts +36 -121
- package/src/index.ts +36 -119
- package/src/inference.ts +128 -0
- package/src/parse.ts +217 -0
- package/src/runtime.ts +137 -0
- package/src/schemas/default.ts +12 -0
- package/src/schemas/discriminated-union.ts +1 -1
- package/src/schemas/nullable.ts +0 -6
- package/src/temporal.ts +0 -1
- package/src/types/any.ts +2 -4
- package/src/types/array.ts +0 -11
- package/src/types/base.ts +45 -139
- package/src/types/boolean.ts +3 -10
- package/src/types/custom.ts +14 -13
- package/src/types/date.ts +13 -1
- package/src/types/enum.ts +12 -15
- package/src/types/literal.ts +3 -5
- package/src/types/never.ts +2 -4
- package/src/types/number.ts +18 -18
- package/src/types/object.ts +107 -42
- package/src/types/string.ts +8 -5
- package/src/types/union.ts +42 -77
package/src/types/boolean.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type StaticDecode,
|
|
4
|
-
type TBoolean,
|
|
5
|
-
Type,
|
|
6
|
-
} from '@sinclair/typebox'
|
|
7
|
-
import { BaseType, type ConstantType } from './base.ts'
|
|
8
|
-
|
|
9
|
-
export class BooleanType extends BaseType<TBoolean> {
|
|
10
|
-
_!: ConstantType<this['schema']>
|
|
1
|
+
import { type TBoolean, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
11
3
|
|
|
4
|
+
export class BooleanType extends BaseType<TBoolean, {}, boolean> {
|
|
12
5
|
static factory() {
|
|
13
6
|
return new BooleanType(Type.Boolean())
|
|
14
7
|
}
|
package/src/types/custom.ts
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type TAny,
|
|
3
|
+
type TSchema,
|
|
3
4
|
type TTransform,
|
|
4
|
-
type TUnsafe,
|
|
5
5
|
Type,
|
|
6
6
|
} from '@sinclair/typebox'
|
|
7
|
-
import {
|
|
7
|
+
import type { StaticInputDecode, StaticOutputDecode } from '../inference.ts'
|
|
8
|
+
import { BaseType } from './base.ts'
|
|
8
9
|
|
|
9
10
|
export type CustomTypeDecode<T> = (value: any) => T
|
|
10
11
|
export type CustomTypeEncode<T> = (value: T) => any
|
|
11
12
|
|
|
12
|
-
export abstract class TransformType<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
13
|
+
export abstract class TransformType<
|
|
14
|
+
T,
|
|
15
|
+
S extends TSchema = TAny,
|
|
16
|
+
> extends BaseType<TTransform<S, T>, {}, StaticInputDecode<TTransform<S, T>>> {}
|
|
17
17
|
|
|
18
|
-
export class CustomType<T, S = TAny> extends TransformType<
|
|
19
|
-
|
|
18
|
+
export class CustomType<T, S extends TSchema = TAny> extends TransformType<
|
|
19
|
+
T,
|
|
20
|
+
S
|
|
21
|
+
> {
|
|
22
|
+
static factory<T, S extends TSchema = TAny>(
|
|
20
23
|
decode: CustomTypeDecode<T>,
|
|
21
24
|
encode: CustomTypeEncode<T>,
|
|
22
|
-
schema = Type.Any() as
|
|
25
|
+
schema: S = Type.Any() as S,
|
|
23
26
|
) {
|
|
24
27
|
return new CustomType<T, S>(
|
|
25
|
-
Type.Transform(schema
|
|
26
|
-
.Decode(decode)
|
|
27
|
-
.Encode(encode),
|
|
28
|
+
Type.Transform(schema).Decode(decode).Encode(encode),
|
|
28
29
|
{},
|
|
29
30
|
{ encode } as any,
|
|
30
31
|
)
|
package/src/types/date.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TString, Type } from '@sinclair/typebox'
|
|
1
2
|
import { CustomType, TransformType } from './custom.ts'
|
|
2
3
|
|
|
3
4
|
const decode = (value: any): Date => new Date(value)
|
|
@@ -5,6 +6,17 @@ const encode = (value: Date): any => value.toISOString()
|
|
|
5
6
|
|
|
6
7
|
export class DateType extends TransformType<Date> {
|
|
7
8
|
static factory() {
|
|
8
|
-
return CustomType.factory<Date>(
|
|
9
|
+
return CustomType.factory<Date, TString>(
|
|
10
|
+
decode,
|
|
11
|
+
encode,
|
|
12
|
+
Type.Union([
|
|
13
|
+
Type.String({
|
|
14
|
+
format: 'date',
|
|
15
|
+
}),
|
|
16
|
+
Type.String({
|
|
17
|
+
format: 'date-time',
|
|
18
|
+
}),
|
|
19
|
+
]) as unknown as TString,
|
|
20
|
+
)
|
|
9
21
|
}
|
|
10
22
|
}
|
package/src/types/enum.ts
CHANGED
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type SchemaOptions,
|
|
4
|
-
type StaticDecode,
|
|
5
|
-
type TEnum,
|
|
6
|
-
Type,
|
|
7
|
-
} from '@sinclair/typebox'
|
|
8
|
-
import { BaseType, type ConstantType } from './base.ts'
|
|
1
|
+
import { type TEnum, Type } from '@sinclair/typebox'
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
9
3
|
|
|
10
4
|
export class ObjectEnumType<
|
|
11
5
|
T extends { [K in string]: K } = { [K in string]: K },
|
|
12
|
-
> extends BaseType<TEnum<T
|
|
13
|
-
_!: ConstantType<this['schema']>
|
|
14
|
-
|
|
6
|
+
> extends BaseType<TEnum<T>, { values: T[keyof T] }, T[keyof T]> {
|
|
15
7
|
static factory<T extends { [K in string]: K }>(values: T) {
|
|
16
|
-
return new ObjectEnumType<T>(Type.Enum(values as any)
|
|
8
|
+
return new ObjectEnumType<T>(Type.Enum(values as any), {
|
|
9
|
+
values: Object.values(values) as unknown as T[keyof T],
|
|
10
|
+
})
|
|
17
11
|
}
|
|
18
12
|
}
|
|
19
13
|
|
|
20
14
|
export class EnumType<
|
|
21
15
|
T extends (string | number)[] = (string | number)[],
|
|
22
|
-
> extends BaseType<
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
> extends BaseType<
|
|
17
|
+
TEnum<Record<string, T[number]>>,
|
|
18
|
+
{ values: [...T] },
|
|
19
|
+
T[keyof T]
|
|
20
|
+
> {
|
|
25
21
|
static factory<T extends (string | number)[]>(values: [...T]) {
|
|
26
22
|
return new EnumType<T>(
|
|
27
23
|
Type.Enum(Object.fromEntries(values.map((v) => [v, v])) as any),
|
|
24
|
+
{ values },
|
|
28
25
|
)
|
|
29
26
|
}
|
|
30
27
|
}
|
package/src/types/literal.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'
|
|
2
|
-
import { BaseType
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
4
|
export class LiteralType<
|
|
5
5
|
T extends TLiteralValue = TLiteralValue,
|
|
6
|
-
> extends BaseType<TLiteral<T
|
|
7
|
-
_!: ConstantType<this['schema']>
|
|
8
|
-
|
|
6
|
+
> extends BaseType<TLiteral<T>, { value: TLiteralValue }, T> {
|
|
9
7
|
static factory<T extends TLiteralValue>(value: T) {
|
|
10
|
-
return new LiteralType<T>(Type.Literal(value))
|
|
8
|
+
return new LiteralType<T>(Type.Literal(value), { value })
|
|
11
9
|
}
|
|
12
10
|
}
|
package/src/types/never.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { type TNever, Type } from '@sinclair/typebox'
|
|
2
|
-
import { BaseType
|
|
3
|
-
|
|
4
|
-
export class NeverType extends BaseType<TNever> {
|
|
5
|
-
_!: ConstantType<this['schema']>
|
|
2
|
+
import { BaseType } from './base.ts'
|
|
6
3
|
|
|
4
|
+
export class NeverType extends BaseType<TNever, {}, never> {
|
|
7
5
|
static factory() {
|
|
8
6
|
return new NeverType(Type.Never())
|
|
9
7
|
}
|
package/src/types/number.ts
CHANGED
|
@@ -2,23 +2,18 @@ import {
|
|
|
2
2
|
type BigIntOptions,
|
|
3
3
|
type IntegerOptions,
|
|
4
4
|
type NumberOptions,
|
|
5
|
-
type Static,
|
|
6
|
-
type StaticDecode,
|
|
7
|
-
type StringOptions,
|
|
8
5
|
type TBigInt,
|
|
9
6
|
type TInteger,
|
|
10
7
|
type TNumber,
|
|
11
|
-
type TString,
|
|
12
|
-
type TTransform,
|
|
13
8
|
Type,
|
|
14
|
-
TypeBoxError,
|
|
15
9
|
} from '@sinclair/typebox'
|
|
16
|
-
import { BaseType
|
|
17
|
-
import { CustomType, TransformType } from './custom.ts'
|
|
18
|
-
|
|
19
|
-
export class NumberType extends BaseType<TNumber, { options: NumberOptions }> {
|
|
20
|
-
_!: ConstantType<this['schema']>
|
|
10
|
+
import { BaseType } from './base.ts'
|
|
21
11
|
|
|
12
|
+
export class NumberType extends BaseType<
|
|
13
|
+
TNumber,
|
|
14
|
+
{ options: NumberOptions },
|
|
15
|
+
number
|
|
16
|
+
> {
|
|
22
17
|
static factory(options: NumberOptions = {}) {
|
|
23
18
|
return new NumberType(Type.Number(options), { options })
|
|
24
19
|
}
|
|
@@ -50,10 +45,9 @@ export class NumberType extends BaseType<TNumber, { options: NumberOptions }> {
|
|
|
50
45
|
|
|
51
46
|
export class IntegerType extends BaseType<
|
|
52
47
|
TInteger,
|
|
53
|
-
{ options: IntegerOptions }
|
|
48
|
+
{ options: IntegerOptions },
|
|
49
|
+
number
|
|
54
50
|
> {
|
|
55
|
-
_!: ConstantType<this['schema']>
|
|
56
|
-
|
|
57
51
|
static factory(options: IntegerOptions = {}) {
|
|
58
52
|
return new IntegerType(Type.Integer(options), { options })
|
|
59
53
|
}
|
|
@@ -84,11 +78,17 @@ export class IntegerType extends BaseType<
|
|
|
84
78
|
}
|
|
85
79
|
|
|
86
80
|
// TODO: this is not json schema compatible
|
|
87
|
-
export class BigIntType extends BaseType<
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
export class BigIntType extends BaseType<
|
|
82
|
+
TBigInt,
|
|
83
|
+
{ options: BigIntOptions },
|
|
84
|
+
bigint
|
|
85
|
+
> {
|
|
90
86
|
static factory(options: BigIntOptions = {}) {
|
|
91
|
-
return new BigIntType(
|
|
87
|
+
return new BigIntType(
|
|
88
|
+
Type.BigInt(options),
|
|
89
|
+
{ options },
|
|
90
|
+
{ encode: (value) => `${value}` },
|
|
91
|
+
)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
positive() {
|
package/src/types/object.ts
CHANGED
|
@@ -5,30 +5,21 @@ import {
|
|
|
5
5
|
type TSchema,
|
|
6
6
|
Type,
|
|
7
7
|
} from '@sinclair/typebox'
|
|
8
|
-
import {
|
|
9
|
-
import type {
|
|
8
|
+
import type { StaticInputDecode } from '../inference.ts'
|
|
9
|
+
import type { UnionToTupleString } from '../utils.ts'
|
|
10
|
+
import { BaseType, type BaseTypeAny, type OptionalType } from './base.ts'
|
|
11
|
+
import { EnumType, type ObjectEnumType } from './enum.ts'
|
|
10
12
|
import type { LiteralType } from './literal.ts'
|
|
11
13
|
import type { StringType } from './string.ts'
|
|
12
14
|
|
|
13
|
-
export type ObjectTypeProps = { [k: string]: BaseTypeAny
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
> extends BaseType<
|
|
15
|
+
export type ObjectTypeProps = { [k: string]: BaseTypeAny }
|
|
16
|
+
export type AnyObjectType = ObjectType<ObjectTypeProps>
|
|
17
|
+
export class ObjectType<T extends ObjectTypeProps = {}> extends BaseType<
|
|
17
18
|
TObject<{ [K in keyof T]: T[K]['schema'] }>,
|
|
18
|
-
{ properties: T }
|
|
19
|
+
{ properties: T },
|
|
20
|
+
StaticInputDecode<TObject<{ [K in keyof T]: T[K]['schema'] }>>
|
|
19
21
|
> {
|
|
20
|
-
|
|
21
|
-
encoded: {
|
|
22
|
-
input: TObject<{ [K in keyof T]: T[K]['_']['encoded']['input'] }>
|
|
23
|
-
output: TObject<{ [K in keyof T]: T[K]['_']['encoded']['output'] }>
|
|
24
|
-
}
|
|
25
|
-
decoded: {
|
|
26
|
-
input: TObject<{ [K in keyof T]: T[K]['_']['decoded']['input'] }>
|
|
27
|
-
output: TObject<{ [K in keyof T]: T[K]['_']['decoded']['output'] }>
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
static factory<T extends ObjectTypeProps = ObjectTypeProps>(
|
|
22
|
+
static factory<T extends ObjectTypeProps = {}>(
|
|
32
23
|
properties: T,
|
|
33
24
|
options: ObjectOptions = {},
|
|
34
25
|
) {
|
|
@@ -50,29 +41,6 @@ export class RecordType<
|
|
|
50
41
|
K extends LiteralType | EnumType | ObjectEnumType | StringType,
|
|
51
42
|
E extends BaseType,
|
|
52
43
|
> extends BaseType<TRecordOrObject<K['schema'], E['schema']>> {
|
|
53
|
-
_!: {
|
|
54
|
-
encoded: {
|
|
55
|
-
input: TRecordOrObject<
|
|
56
|
-
K['_']['encoded']['input'],
|
|
57
|
-
E['_']['encoded']['input']
|
|
58
|
-
>
|
|
59
|
-
output: TRecordOrObject<
|
|
60
|
-
K['_']['encoded']['output'],
|
|
61
|
-
E['_']['encoded']['output']
|
|
62
|
-
>
|
|
63
|
-
}
|
|
64
|
-
decoded: {
|
|
65
|
-
input: TRecordOrObject<
|
|
66
|
-
K['_']['decoded']['input'],
|
|
67
|
-
E['_']['decoded']['input']
|
|
68
|
-
>
|
|
69
|
-
output: TRecordOrObject<
|
|
70
|
-
K['_']['decoded']['output'],
|
|
71
|
-
E['_']['decoded']['output']
|
|
72
|
-
>
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
44
|
static factory<
|
|
77
45
|
K extends
|
|
78
46
|
| LiteralType<any>
|
|
@@ -86,3 +54,100 @@ export class RecordType<
|
|
|
86
54
|
)
|
|
87
55
|
}
|
|
88
56
|
}
|
|
57
|
+
|
|
58
|
+
export function keyof<T extends ObjectType>(
|
|
59
|
+
type: T,
|
|
60
|
+
): EnumType<
|
|
61
|
+
UnionToTupleString<T extends ObjectType<infer Props> ? keyof Props : never>
|
|
62
|
+
> {
|
|
63
|
+
return EnumType.factory(Object.keys(type.props.properties) as any)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function pick<
|
|
67
|
+
T extends AnyObjectType,
|
|
68
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
69
|
+
>(
|
|
70
|
+
source: T,
|
|
71
|
+
pick: P,
|
|
72
|
+
): ObjectType<{
|
|
73
|
+
[K in keyof P]: P[K] extends true
|
|
74
|
+
? K extends keyof T['props']['properties']
|
|
75
|
+
? T['props']['properties'][K]
|
|
76
|
+
: never
|
|
77
|
+
: never
|
|
78
|
+
}> {
|
|
79
|
+
const properties = Object.fromEntries(
|
|
80
|
+
Object.entries(source.props.properties).filter(([key]) => pick[key]),
|
|
81
|
+
)
|
|
82
|
+
return ObjectType.factory(properties) as any
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function omit<
|
|
86
|
+
T extends AnyObjectType,
|
|
87
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
88
|
+
>(
|
|
89
|
+
source: T,
|
|
90
|
+
omit: P,
|
|
91
|
+
): ObjectType<{
|
|
92
|
+
[K in keyof T['props']['properties'] as K extends keyof P
|
|
93
|
+
? never
|
|
94
|
+
: K]: T['props']['properties'][K]
|
|
95
|
+
}> {
|
|
96
|
+
const properties = Object.fromEntries(
|
|
97
|
+
Object.entries(source.props.properties).filter(([key]) => !omit[key]),
|
|
98
|
+
)
|
|
99
|
+
return ObjectType.factory(properties) as any
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function extend<T extends AnyObjectType, P extends ObjectTypeProps>(
|
|
103
|
+
object1: T,
|
|
104
|
+
properties: P,
|
|
105
|
+
): ObjectType<{
|
|
106
|
+
[K in keyof T['props']['properties'] | keyof P]: K extends keyof P
|
|
107
|
+
? P[K]
|
|
108
|
+
: K extends keyof T['props']['properties']
|
|
109
|
+
? T['props']['properties'][K]
|
|
110
|
+
: never
|
|
111
|
+
}> {
|
|
112
|
+
return ObjectType.factory({
|
|
113
|
+
...object1.props.properties,
|
|
114
|
+
...properties,
|
|
115
|
+
}) as any
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function merge<T1 extends AnyObjectType, T2 extends AnyObjectType>(
|
|
119
|
+
object1: T1,
|
|
120
|
+
object2: T2,
|
|
121
|
+
): ObjectType<{
|
|
122
|
+
[K in
|
|
123
|
+
| keyof T1['props']['properties']
|
|
124
|
+
| keyof T2['props']['properties']]: K extends keyof T2['props']['properties']
|
|
125
|
+
? T2['props']['properties'][K]
|
|
126
|
+
: K extends keyof T1['props']['properties']
|
|
127
|
+
? T1['props']['properties'][K]
|
|
128
|
+
: never
|
|
129
|
+
}> {
|
|
130
|
+
return ObjectType.factory({
|
|
131
|
+
...object1.props.properties,
|
|
132
|
+
...object2.props.properties,
|
|
133
|
+
}) as any
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function partial<
|
|
137
|
+
T extends AnyObjectType,
|
|
138
|
+
P extends T extends ObjectType<infer Props> ? Props : never,
|
|
139
|
+
>(
|
|
140
|
+
object: T,
|
|
141
|
+
): ObjectType<{
|
|
142
|
+
[K in keyof P]: P[K] extends BaseType<any, any, infer T>
|
|
143
|
+
? OptionalType<P[K], T>
|
|
144
|
+
: never
|
|
145
|
+
}> {
|
|
146
|
+
const properties = {} as any
|
|
147
|
+
|
|
148
|
+
for (const [key, value] of Object.entries(object.props.properties)) {
|
|
149
|
+
properties[key] = value.optional()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return ObjectType.factory(properties, {}) as any
|
|
153
|
+
}
|
package/src/types/string.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { type StringOptions, type TString, Type } from '@sinclair/typebox'
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import type { StaticOutputDecode } from '../inference.ts'
|
|
3
|
+
import { BaseType } from './base.ts'
|
|
4
|
+
|
|
5
|
+
export class StringType extends BaseType<
|
|
6
|
+
TString,
|
|
7
|
+
{ options: StringOptions },
|
|
8
|
+
string
|
|
9
|
+
> {
|
|
7
10
|
static factory(options: StringOptions = {}) {
|
|
8
11
|
return new StringType(Type.String(options), { options })
|
|
9
12
|
}
|
package/src/types/union.ts
CHANGED
|
@@ -1,57 +1,52 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type TIntersect,
|
|
3
|
+
type TObject,
|
|
4
|
+
type TSchema,
|
|
3
5
|
type TUnion,
|
|
4
6
|
Type,
|
|
5
7
|
type UnionToTuple,
|
|
6
8
|
} from '@sinclair/typebox'
|
|
9
|
+
import type { StaticInputDecode } from '../inference.ts'
|
|
7
10
|
import {
|
|
8
11
|
DiscriminatedUnion,
|
|
12
|
+
type DiscriminatedUnionProperties,
|
|
9
13
|
type TDiscriminatedUnion,
|
|
10
14
|
} from '../schemas/discriminated-union.ts'
|
|
11
15
|
import { BaseType, type BaseTypeAny } from './base.ts'
|
|
12
|
-
import type { LiteralType } from './literal.ts'
|
|
13
16
|
import type { ObjectType, ObjectTypeProps } from './object.ts'
|
|
14
17
|
|
|
15
18
|
export class UnionType<
|
|
16
19
|
T extends readonly BaseType[] = readonly BaseType[],
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
static factory<T extends readonly BaseType[] = readonly BaseType[]>(
|
|
30
|
-
...types: T
|
|
31
|
-
) {
|
|
32
|
-
return new UnionType<T>(Type.Union(types.map((t) => t.schema)) as any)
|
|
20
|
+
S extends TSchema[] = UnionToTuple<T[number]['schema']>,
|
|
21
|
+
> extends BaseType<TUnion<S>, { options: T }, StaticInputDecode<TUnion<S>>> {
|
|
22
|
+
static factory<
|
|
23
|
+
T extends readonly BaseType[] = readonly BaseType[],
|
|
24
|
+
S extends TSchema[] = UnionToTuple<T[number]['schema']>,
|
|
25
|
+
>(...options: T) {
|
|
26
|
+
return new UnionType<T, S>(
|
|
27
|
+
Type.Union(options.map((t) => t.schema)) as any,
|
|
28
|
+
{
|
|
29
|
+
options,
|
|
30
|
+
},
|
|
31
|
+
)
|
|
33
32
|
}
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
export class IntersactionType<
|
|
37
36
|
T extends readonly BaseType[] = readonly BaseType[],
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
...types: T
|
|
52
|
-
) {
|
|
53
|
-
return new IntersactionType<T>(
|
|
54
|
-
Type.Intersect(types.map((t) => t.schema)) as any,
|
|
37
|
+
S extends TSchema[] = UnionToTuple<T[number]['schema']>,
|
|
38
|
+
> extends BaseType<
|
|
39
|
+
TIntersect<S>,
|
|
40
|
+
{ options: T },
|
|
41
|
+
StaticInputDecode<TIntersect<S>>
|
|
42
|
+
> {
|
|
43
|
+
static factory<
|
|
44
|
+
T extends readonly BaseType[] = readonly BaseType[],
|
|
45
|
+
S extends TSchema[] = UnionToTuple<T[number]['schema']>,
|
|
46
|
+
>(...options: T) {
|
|
47
|
+
return new IntersactionType<T, S>(
|
|
48
|
+
Type.Intersect(options.map((t) => t.schema)) as any,
|
|
49
|
+
{ options },
|
|
55
50
|
)
|
|
56
51
|
}
|
|
57
52
|
}
|
|
@@ -61,57 +56,27 @@ export type DiscriminatedUnionOptionType<K extends string> = ObjectType<
|
|
|
61
56
|
>
|
|
62
57
|
|
|
63
58
|
export class DiscriminatedUnionType<
|
|
64
|
-
K extends string,
|
|
65
|
-
T extends
|
|
66
|
-
DiscriminatedUnionOptionType<K
|
|
67
|
-
|
|
68
|
-
],
|
|
59
|
+
K extends string = string,
|
|
60
|
+
T extends
|
|
61
|
+
readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],
|
|
62
|
+
S extends TObject<DiscriminatedUnionProperties<K>>[] = [],
|
|
69
63
|
> extends BaseType<
|
|
70
|
-
TDiscriminatedUnion<
|
|
71
|
-
K,
|
|
72
|
-
//@ts-expect-error
|
|
73
|
-
UnionToTuple<T[number]['schema']>
|
|
74
|
-
>,
|
|
64
|
+
TDiscriminatedUnion<K, S>,
|
|
75
65
|
{
|
|
76
66
|
key: K
|
|
77
67
|
options: T
|
|
78
|
-
}
|
|
68
|
+
},
|
|
69
|
+
StaticInputDecode<TDiscriminatedUnion<K, S>>
|
|
79
70
|
> {
|
|
80
|
-
_!: {
|
|
81
|
-
encoded: {
|
|
82
|
-
input: TDiscriminatedUnion<
|
|
83
|
-
K,
|
|
84
|
-
//@ts-expect-error
|
|
85
|
-
UnionToTuple<T[number]['_']['encoded']['input']>
|
|
86
|
-
>
|
|
87
|
-
output: TDiscriminatedUnion<
|
|
88
|
-
K,
|
|
89
|
-
//@ts-expect-error
|
|
90
|
-
UnionToTuple<T[number]['_']['encoded']['output']>
|
|
91
|
-
>
|
|
92
|
-
}
|
|
93
|
-
decoded: {
|
|
94
|
-
input: TDiscriminatedUnion<
|
|
95
|
-
K,
|
|
96
|
-
//@ts-expect-error
|
|
97
|
-
UnionToTuple<T[number]['_']['decoded']['input']>
|
|
98
|
-
>
|
|
99
|
-
output: TDiscriminatedUnion<
|
|
100
|
-
K,
|
|
101
|
-
//@ts-expect-error
|
|
102
|
-
UnionToTuple<T[number]['_']['decoded']['output']>
|
|
103
|
-
>
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
71
|
static factory<
|
|
108
72
|
K extends string,
|
|
109
|
-
T extends readonly [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
73
|
+
T extends readonly DiscriminatedUnionOptionType<K>[],
|
|
74
|
+
//@ts-expect-error
|
|
75
|
+
S extends TObject<DiscriminatedUnionProperties<K>>[] = UnionToTuple<
|
|
76
|
+
T[number]['schema']
|
|
77
|
+
>,
|
|
113
78
|
>(key: K, ...options: T) {
|
|
114
|
-
return new DiscriminatedUnionType<K, T>(
|
|
79
|
+
return new DiscriminatedUnionType<K, T, S>(
|
|
115
80
|
DiscriminatedUnion(key, options.map((t) => t.schema) as any),
|
|
116
81
|
{ key, options },
|
|
117
82
|
)
|