From bbb62a63f47e1f563864979d7ef920b92eb7783b Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Mon, 8 Jan 2018 14:13:52 -0800 Subject: [PATCH] Fixes [yup] invalid types for addMethod #22721 (#22736) --- types/yup/index.d.ts | 10 +++++++++- types/yup/yup-tests.ts | 9 ++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index 97c9618428..6c03557c60 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -5,7 +5,7 @@ // TypeScript Version: 2.2 export function reach(schema: Schema, path: string, value?: any, context?: any): Schema; -export function addMethod(schemaType: Schema, name: string, method: (this: Schema) => Schema): void; +export function addMethod(schemaCtor: AnySchemaConstructor, name: string, method: (this: T, ...args: any[]) => T): void; export function ref(path: string, options?: { contextPrefix: string }): Ref; export function lazy(fn: (value: any) => Schema): Lazy; export function ValidationError(errors: string | string[], value: any, path: string, type?: any): ValidationError; @@ -19,6 +19,14 @@ export const date: DateSchemaConstructor; export const array: ArraySchemaConstructor; export const object: ObjectSchemaConstructor; +export type AnySchemaConstructor = MixedSchemaConstructor + | StringSchemaConstructor + | NumberSchemaConstructor + | BooleanSchemaConstructor + | DateSchemaConstructor + | ArraySchemaConstructor + | ObjectSchemaConstructor; + export interface Schema { clone(): this; label(label: string): this; diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index 7fc65b09f2..24c47fb418 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -1,6 +1,6 @@ import * as yup from 'yup'; // tslint:disable-next-line:no-duplicate-imports -import { reach, date, Schema, ObjectSchema, ValidationError, MixedSchema, SchemaDescription, TestOptions, ValidateOptions } from 'yup'; +import { reach, date, Schema, ObjectSchema, ValidationError, MixedSchema, SchemaDescription, TestOptions, ValidateOptions, NumberSchema } from 'yup'; // reach function let schema = yup.object().shape({ @@ -14,8 +14,11 @@ reach(schema, 'nested.arr.num'); reach(schema, 'nested.arr[].num'); // addMethod function -yup.addMethod(yup.date(), 'format', function(this: Schema) { - return this.clone(); +yup.addMethod(yup.number, 'minimum', function(this, minValue: number, message: string) { + return this.min(minValue, message); +}); +yup.addMethod(yup.date, 'newMethod', function(this: yup.DateSchema, date: Date, message?: string) { + return this.max(date, message); }); // ref function