Cuando se agrega un método personalizado a la instancia de yup usando la función addMethod, se produce el siguiente error
TS2339: la propiedad 'título' no existe en el tipo 'tipo de importación ("node_modules/yup/lib/index")'
Reproducir
archivo yupInstance.ts
import * as yup from 'yup'; function defaultTitleValidation(this: any, local: 'en' | 'bn') { return this.string().trim().required(); } yup.addMethod(yup.string, 'title', defaultTitleValidation); export default yup;archivo common.d.ts
declare module 'yup' { interface StringSchema<TIn, TContext, TOut> { title(local: 'en' | 'bn'): any; } }myImplementationComponent.tsx
import yup from '../../../../common/yup'; const validationSchema = yup.object().shape({ title_en: yup.title(), // TS2339: Property 'title' does not exist on type 'typeof import("node_modules/yup/lib/index")' });Resuelto extendiendo la interfaz yup.BaseSchema .
declare module 'yup' { interface StringSchema< TType extends Maybe<string> = string | undefined, TContext extends AnyObject = AnyObject, TOut extends TType = TType, > extends yup.BaseSchema<TType, TContext, TOut> { title(local: 'en' | 'bn'): StringSchema<TType, TContext>; } }Método personalizado
function defaultTitleValidation(this: any, local: 'en' | 'bn') { return this.trim().required(); //before this.string().trim().required(); }Implementación
const validationSchema = yup.object().shape({ title_en: yup.string().title('en'), //before yup.title(), });