Skip to content

Commit

Permalink
fix: array handling in SchemaOf type (#1169)
Browse files Browse the repository at this point in the history
- Move array before object so that inference works correctly
- Include lazy as part of array type, to match the class definition
  • Loading branch information
chasecaleb authored Dec 10, 2020
1 parent 2d71f32 commit e785e1a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DateSchema, { create as dateCreate } from './date';
import ObjectSchema, { AnyObject, create as objectCreate } from './object';
import ArraySchema, { create as arrayCreate } from './array';
import { create as refCreate } from './Reference';
import { create as lazyCreate } from './Lazy';
import Lazy, { create as lazyCreate } from './Lazy';
import ValidationError from './ValidationError';
import reach from './util/reach';
import isSchema from './util/isSchema';
Expand Down Expand Up @@ -39,20 +39,20 @@ function addMethod(schemaType: any, name: string, fn: any) {

type ObjectSchemaOf<T extends AnyObject> = ObjectSchema<
{
[k in keyof T]-?: T[k] extends AnyObject
[k in keyof T]-?: T[k] extends Array<infer E>
? ArraySchema<SchemaOf<E>>
: T[k] extends AnyObject
? // we can't use ObjectSchema<{ []: SchemaOf<T[k]> }> b/c TS produces a union of two schema
ObjectSchemaOf<T[k]>
: T[k] extends Array<infer E>
? ArraySchema<SchemaOf<E>>
: BaseSchema<Maybe<T[k]>, AnyObject, T[k]>;
}
>;

type SchemaOf<T> = T extends AnyObject
? ObjectSchemaOf<T>
: T extends Array<infer E>
? ArraySchema<SchemaOf<E>>
: BaseSchema<Maybe<T>, AnyObject, T>;
type SchemaOf<T> = T extends Array<infer E>
? ArraySchema<SchemaOf<E> | Lazy<SchemaOf<E>>>
: T extends AnyObject
? ObjectSchemaOf<T>
: BaseSchema<Maybe<T>, AnyObject, T>;

export type AnyObjectSchema = ObjectSchema<any, any, any, any>;

Expand Down

0 comments on commit e785e1a

Please sign in to comment.