当使用 addMethod 函数将自定义方法添加到 yup 实例时,会产生以下错误
TS2339:类型“typeof import("node_modules/yup/lib/index")”上不存在属性“title”
重现
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;
Run Code Online (Sandbox Code Playgroud)
common.d.ts 文件
declare module 'yup' {
interface StringSchema<TIn, TContext, TOut> {
title(local: 'en' | 'bn'): any;
}
}
Run Code Online (Sandbox Code Playgroud)
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")'
});
Run Code Online (Sandbox Code Playgroud)