我正在使用Yup进行表单验证的TypeScript项目中工作。我遇到了when
函数和is
条件的问题。TypeScript引发了与类型相关的错误(TS2769),我在解决该错误时遇到了问题。
export type TClubFormData = {
title: string;
description: string;
info: string;
city: string;
address: string;
phone: string;
picture: File | null;
pictureUrl: string;
latitudeMap: string;
longitudeMap: string;
openingTime: string;
closingTime: string;
};
x
const initialValues: TClubFormData = {
title: (club && club.title) || '',
description: (club && club.description) || '',
info: (club && club.info) || '',
address: (club && club.address) || '',
city: (club && club.city) || '',
phone: (club && club.phone) || '',
picture: null,
pictureUrl: (club && club.picture) || '',
latitudeMap: (club && club.latitudeMap) || '',
longitudeMap: (club && club.longitudeMap) || '',
openingTime: (club && club.openingTime) || '00:00',
closingTime: (club && club.closingTime) || '00:00',
};
export const CreateAndEditClubFormSchema = Yup.object().shape({
title: Yup.string().required('Անվանումը պարտադիր է'),
description: Yup.string().required('Նկարագիրը պարտադիր է'),
info: Yup.string(),
city: Yup.string().required('Քաղաքը պարտադիր է'),
address: Yup.string().required('Հասցեն պարտադիր է'),
phone: Yup.string(),
// picture: Yup.mixed().nullable().required('Նկարը պարտադիր է'),
picture: Yup.mixed().when('pictureUrl', {
is: (pictureUrl: string) => !pictureUrl, // ERROR HERE
then: Yup.mixed().nullable().required('Նկարը պարտադիր է'),
otherwise: Yup.mixed().nullable(),
}),
latitudeMap: Yup.string().matches(
/^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?[0-8]\d((\.)|\.\d{1,6})?)|(0*?90((\.)|\.0{1,6})?))$/,
'Սխալ կորդինատային լայնություն'
),
longitudeMap: Yup.string().matches(
/^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?\d\d((\.)|\.\d{1,6})?)|(0*?1[0-7]\d((\.)|\.\d{1,6})?)|(0*?180((\.)|\.0{1,6})?))$/,
'Սխալ կորդինատային երկայնություն'
),
openingTime: Yup.string().required('Opening time is required'),
closingTime: Yup.string().required('Closing time is required'),
});
TS2769: No overload matches this call.
Overload 1 of 4 ,
(keys: string | string[], builder: ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>): MixedSchema<...>
, gave the following error.
Argument of type
{ is: (pictureUrl: string) => boolean; then: Yup.MixedSchema<AnyPresentValue, Yup.AnyObject, undefined, "">; otherwise: Yup.MixedSchema<AnyPresentValue | null | undefined, Yup.AnyObject, undefined, "">; }
is not assignable to parameter of type
ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>
Object literal may only specify known properties, and is does not exist in type
ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>
的数据
如何解决此错误?
1条答案
按热度按时间xvw2m8pv1#
这对我很有用
字符串