Skip to content

Commit

Permalink
fix(types): array type with lazy
Browse files Browse the repository at this point in the history
fixes #1146
  • Loading branch information
jquense committed Dec 7, 2020
1 parent b38219f commit ba92dfc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import BaseSchema, {
SchemaInnerTypeDescription,
SchemaSpec,
} from './schema';
import Lazy from './Lazy';

export type RejectorFn = (value: any, index: number, array: any[]) => boolean;

export function create<
C extends AnyObject = AnyObject,
T extends AnySchema = AnySchema
T extends AnySchema | Lazy<any, any> = AnySchema
>(type?: T) {
return new ArraySchema<T, C>(type) as OptionalArraySchema<T, C>;
}

export default class ArraySchema<
T extends AnySchema,
T extends AnySchema | Lazy<any, any>,
C extends AnyObject = AnyObject,
TIn extends Maybe<TypeOf<T>[]> = TypeOf<T>[] | undefined,
TOut extends Maybe<Asserts<T>[]> = Asserts<T>[] | Optionals<TIn>
Expand Down Expand Up @@ -135,13 +136,7 @@ export default class ArraySchema<
originalValue: originalValue[idx],
};

tests[idx] = (_, cb) =>
innerType!.validate(
item,
innerOptions,
// @ts-expect-error
cb,
);
tests[idx] = (_, cb) => innerType!.validate(item, innerOptions, cb);
}

runTests(
Expand Down Expand Up @@ -175,7 +170,8 @@ export default class ArraySchema<

if (schema.innerType)
next.innerType = next.innerType
? next.innerType.concat(schema.innerType)
? // @ts-expect-error Lazy doesn't have concat()
next.innerType.concat(schema.innerType)
: schema.innerType;

return next;
Expand Down Expand Up @@ -274,7 +270,7 @@ create.prototype = ArraySchema.prototype;
//

export interface DefinedArraySchema<
T extends AnySchema,
T extends AnySchema | Lazy<any, any>,
TContext extends AnyObject,
TIn extends Maybe<TypeOf<T>[]>
> extends ArraySchema<T, TContext, TIn, Asserts<T>[] | Preserve<TIn, null>> {
Expand All @@ -299,7 +295,7 @@ export interface DefinedArraySchema<
}

export interface RequiredArraySchema<
T extends AnySchema,
T extends AnySchema | Lazy<any, any>,
TContext extends AnyObject,
TIn extends Maybe<TypeOf<T>[]>
> extends ArraySchema<T, TContext, TIn, Asserts<T>[]> {
Expand All @@ -322,7 +318,7 @@ export interface RequiredArraySchema<
}

export interface OptionalArraySchema<
T extends AnySchema,
T extends AnySchema | Lazy<any, any>,
TContext extends AnyObject = AnyObject,
TIn extends Maybe<TypeOf<T>[]> = TypeOf<T>[] | undefined
> extends ArraySchema<T, TContext, TIn> {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type SchemaOf<T> = T extends AnyObject
? ArraySchema<SchemaOf<E>>
: BaseSchema<Maybe<T>, AnyObject, T>;

export type AnyObjectSchema = ObjectSchema<any, any, any>;
export type { SchemaOf, TypeOf, Asserts, Asserts as InferType, AnySchema };

export {
Expand Down
3 changes: 3 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ SchemaOf: {
.default(() => [] as string[])
.validateSync(null);

// $ExpectType string[] | undefined
array(lazy(() => string().default(''))).validateSync(null);

const numList = [1, 2];

// $ExpectType (number | undefined)[]
Expand Down

0 comments on commit ba92dfc

Please sign in to comment.