是的,对象是不可分配的,但为什么呢?

yup object is not assignable, but why?

提问人:meds 提问时间:11/5/2023 更新时间:11/5/2023 访问量:41

问:

从此代码:

import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';

interface IFormInputs {
    firstName: string; 
}

const validationSchema = Yup.object({
    firstName: Yup.string().required(), 
});

const Form = () => {
    const { register, handleSubmit, formState: { errors } } = useForm<IFormInputs>({
        resolver: yupResolver(validationSchema),
    });

    const onSubmit = (data: IFormInputs) => {
        console.log(data);
    };

    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <input {...register('firstName')} />
            <p>{errors.firstName?.message}</p>
            <input type="submit" />
        </form>
    );
};

export default Form;

解析程序:错误

  Types of parameters 'options' and 'options' are incompatible.
    Type 'ResolverOptions<IFormInputs>' is not assignable to type 'ResolverOptions<AssertsShape<{ firstName: unknown; }>>'.
      Type 'IFormInputs' is not assignable to type 'AssertsShape<{ firstName: unknown; }>'.ts(2322)
form.d.ts(64, 5): The expected type comes from property 'resolver' which is declared here on type 'Partial<{ mode: keyof ValidationMode; reValidateMode: "onBlur" | "onChange" | "onSubmit"; defaultValues: AsyncDefaultValues<IFormInputs> | { ...; }; ... 9 more ...; delayError: number; }>'
,

和 yupResolver 错误中的 validationSchema,并显示:

  Type 'ObjectSchema<{ firstName: string; }, AnyObject, { firstName: undefined; }, "">' is missing the following properties from type 'ObjectSchema<{ firstName: unknown; }, AnyObject, TypeOfShape<{ firstName: unknown; }>, AssertsShape<{ firstName: unknown; }>>': getDefaultFromShape, __inputType, _isPresentts(2345)
,

,
JavaScript TypeScript 是的

评论


答: 暂无答案