Comparing version 0.0.34 to 0.0.35
@@ -19,3 +19,3 @@ export declare abstract class Type<T> { | ||
export declare type Infer<T extends AnyType> = T extends Type<infer K> ? Eval<K> : any; | ||
export declare type IntersectionResult<T extends AnyType, K extends AnyType> = T extends ObjectType<any> ? K extends ObjectType<any> ? T extends ObjectType<infer Shape1> ? K extends ObjectType<infer Shape2> ? ObjectType<Eval<MergeShapes<Shape1, Shape2>>> : IntersectionType<T, K> : IntersectionType<T, K> : IntersectionType<T, K> : T extends ArrayType<any> ? K extends ArrayType<any> ? T extends ArrayType<infer S1> ? K extends ArrayType<infer S2> ? ArrayType<IntersectionResult<S1, S2>> : IntersectionType<T, K> : IntersectionType<T, K> : IntersectionType<T, K> : IntersectionType<T, K>; | ||
export declare type IntersectionResult<T extends AnyType, K extends AnyType> = T extends ObjectType<any> ? K extends ObjectType<any> ? T extends ObjectType<infer Shape1> ? K extends ObjectType<infer Shape2> ? ObjectType<Eval<MergeShapes<Shape1, Shape2>>> : never : never : IntersectionType<T, K> : T extends ArrayType<any> ? K extends ArrayType<any> ? T extends ArrayType<infer S1> ? K extends ArrayType<infer S2> ? ArrayType<IntersectionResult<S1, S2>> : never : never : IntersectionType<T, K> : T extends TupleType<any> ? K extends TupleType<any> ? T extends TupleType<infer S1> ? K extends TupleType<infer S2> ? TupleType<Join<S1, S2>> : never : never : IntersectionType<T, K> : IntersectionType<T, K>; | ||
export declare type StringOptions = Partial<{ | ||
@@ -168,10 +168,18 @@ pattern: RegExp; | ||
} | ||
declare type InferTuple<T extends [AnyType, ...AnyType[]] | []> = { | ||
declare type IntersecWrapper<A extends any, B extends any> = A extends AnyType ? B extends AnyType ? IntersectionResult<A, B> : never : never; | ||
declare type JoinLeft<A extends AnyType[], B extends AnyType[]> = { | ||
[idx in keyof A]: idx extends keyof B ? IntersecWrapper<A[idx], B[idx]> : A[idx]; | ||
}; | ||
declare type JoinRight<A extends AnyType[], B extends AnyType[]> = { | ||
[idx in keyof B]: idx extends keyof A ? IntersecWrapper<A[idx], B[idx]> : B[idx]; | ||
}; | ||
declare type Join<A extends AnyType[], B extends AnyType[]> = JoinLeft<A, B> & JoinRight<A, B>; | ||
declare type InferTuple<T extends AnyType[]> = { | ||
[key in keyof T]: T[key] extends Type<infer K> ? K : never; | ||
}; | ||
export declare class TupleType<T extends [AnyType, ...AnyType[]] | []> extends Type<InferTuple<T>> { | ||
export declare class TupleType<T extends AnyType[]> extends Type<InferTuple<T>> { | ||
private readonly schemas; | ||
constructor(schemas: T); | ||
parse(value: unknown): InferTuple<T>; | ||
and<K extends AnyType>(schema: K): IntersectionType<this, K>; | ||
and<K extends AnyType>(schema: K): K extends TupleType<any> ? K extends TupleType<infer Arr> ? TupleType<Join<T, Arr>> : never : IntersectionType<this, K>; | ||
} | ||
@@ -178,0 +186,0 @@ declare type InferTupleUnion<T extends any[]> = Infer<T[number]>; |
@@ -49,3 +49,3 @@ "use strict"; | ||
const shapekeysSymbol = Symbol.for('shapeKeys'); | ||
const coercionTypeSybol = Symbol.for('coersion'); | ||
const coercionTypeSybol = Symbol.for('coercion'); | ||
class StringType extends Type { | ||
@@ -526,2 +526,20 @@ constructor(opts = {}) { | ||
and(schema) { | ||
if (schema instanceof TupleType) { | ||
const otherSchemaArray = schema.schemas; | ||
const nextSchemasArray = []; | ||
for (let i = 0; i < Math.max(this.schemas.length, otherSchemaArray.length); i++) { | ||
const current = this.schemas[i]; | ||
const other = otherSchemaArray[i]; | ||
if (current && other) { | ||
nextSchemasArray.push(current.and(other)); | ||
} | ||
else if (current) { | ||
nextSchemasArray.push(current); | ||
} | ||
else { | ||
nextSchemasArray.push(other); | ||
} | ||
} | ||
return new TupleType(nextSchemasArray); | ||
} | ||
return new IntersectionType(this, schema); | ||
@@ -536,2 +554,3 @@ } | ||
this.opts = opts; | ||
this[coercionTypeSybol] = schemas.some(schema => schema[coercionTypeSybol]); | ||
} | ||
@@ -571,5 +590,2 @@ parse(value) { | ||
this._parse = (() => { | ||
if (this.left instanceof TupleType || this.right instanceof TupleType) { | ||
throw new Error('tuple intersection not supported'); | ||
} | ||
// TODO Investigate why I unwrap partials in a new intersection again | ||
@@ -679,3 +695,3 @@ if (this.left instanceof PartialType) { | ||
this.fn = fn; | ||
// Since we can't know what the schema is we can't assume its not a coersionType and we need to disable the optimization | ||
// Since we can't know what the schema is we can't assume its not a coercionType and we need to disable the optimization | ||
this[coercionTypeSybol] = true; | ||
@@ -682,0 +698,0 @@ } |
{ | ||
"name": "myzod", | ||
"version": "0.0.34", | ||
"version": "0.0.35", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
64025
1133