diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index 0e4e5fb5f8..da87f2b9d7 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -210,7 +210,13 @@ interface BasicArraySchema 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, + index: number, + array: Array> + ) => boolean + ): this; } export interface NotRequiredNullableArraySchema @@ -506,3 +512,4 @@ type KeyOfUndefined = { type RequiredProps = Pick>>; type NotRequiredProps = Partial>>; type InnerInferType = NotRequiredProps & RequiredProps; +type InferredArrayType = T extends Array ? U : T; diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index 8de2e8dbbe..c5e33a4648 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -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 = ((x?: T): T => x!)(); const validArr: yup.ArraySchema = arr;