fix array.compact rejector type (#36449)

This commit is contained in:
Paweł Dąbrowski
2019-06-26 09:47:02 -07:00
committed by Benjamin Lichtman
parent e627b5b66b
commit fc01ec61b4
2 changed files with 17 additions and 1 deletions
+8 -1
View File
@@ -210,7 +210,13 @@ interface BasicArraySchema<T extends any[] | null | undefined>
min(limit: number | Ref, message?: TestOptionsMessage): this;
max(limit: number | Ref, message?: TestOptionsMessage): this;
ensure(): this;
compact(rejector?: (value: T, index: number, array: T[]) => boolean): this;
compact(
rejector?: (
value: InferredArrayType<T>,
index: number,
array: Array<InferredArrayType<T>>
) => boolean
): this;
}
export interface NotRequiredNullableArraySchema<T>
@@ -506,3 +512,4 @@ type KeyOfUndefined<T> = {
type RequiredProps<T> = Pick<T, Exclude<keyof T, KeyOfUndefined<T>>>;
type NotRequiredProps<T> = Partial<Pick<T, KeyOfUndefined<T>>>;
type InnerInferType<T> = NotRequiredProps<T> & RequiredProps<T>;
type InferredArrayType<T> = T extends Array<infer U> ? U : T;
+9
View File
@@ -352,6 +352,15 @@ arrSchema.min(5, 'min');
arrSchema.min(5, () => 'min');
arrSchema.compact((value, index, array) => value === array[index]);
const arrOfObjSchema = yup.array().of(
yup.object().shape({
field: yup.number()
})
);
arrOfObjSchema.compact((value, index, array) => {
return value.field > 10 && array[index].field > 10;
});
const arr = yup.array();
const top = (<T>(x?: T): T => x!)();
const validArr: yup.ArraySchema<typeof top> = arr;