输入类型的输入参数formik.error和formik.touched(prime react+ formik)

发布于 2025-01-24 15:28:20 字数 930 浏览 4 评论 0原文

我正在使用TypeScript进行PrimeReAtct,现在我必须将Formik用作Formik form验证器。 primerereAtct(v7)formik文档: https://www.primeg/primeg/primeg/primeface.org/primefaces.org/primeface.orprimeface.org/primereact-v7, /#/formik 当您将选项卡切换到TS源时,没有任何更改。 我对部分有问题:

const isFormFieldValid = (name) => !!(formik.touched[name] && formik.errors[name]);
const getFormErrorMessage = (name) => {
    return isFormFieldValid(name) && <small className="p-error">{formik.errors[name]}</small>;
};

我无法求解“名称”类型。我尝试了:string,formiktouched,任何formikerror ... 当我尝试将字符串作为参数类型时,我的IDE说:

ts7053:元素隐式具有“任何”类型,因为类型'string'的表达式不能用于索引类型'formikTouched&lt; {name:string; }&gt;'。 &nbsp;&nbsp; no索引签名,在类型“ formiktouched&lt; {name:string; string; }&gt;'。

有人可以帮我吗?

I am using PrimeReact with Typescript and now I have to use Formik as form validator.
PrimeReact(v7) Formik documentation: https://www.primefaces.org/primereact-v7/#/formik
and there are no changes when you switch the tab to TS source.
I have problem with part:

const isFormFieldValid = (name) => !!(formik.touched[name] && formik.errors[name]);
const getFormErrorMessage = (name) => {
    return isFormFieldValid(name) && <small className="p-error">{formik.errors[name]}</small>;
};

I cannot solve the "name" type. I tried for a :string, FormikTouched, any, FormikError...
When I try string as type of parameter my IDE say:

TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'FormikTouched<{ name: string; }>'.   No index signature with a parameter of type 'string' was found on type 'FormikTouched<{ name: string; }>'.

Could someone help me with this please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2025-01-31 15:28:20

我这样做了:

  const formikTouched: any = formik.touched;
  const formikErrors: any = formik.errors;

  const isFormFieldValid = (name: string) => !!(formikTouched[name] && formikErrors[name]);
  const getFormErrorMessage = (name: string) => {
    return isFormFieldValid(name) && <small className="p-error">{formikErrors[name]}</small>;
  };

打字稿停止抱怨,现在正在运行而没有错误。

I did this:

  const formikTouched: any = formik.touched;
  const formikErrors: any = formik.errors;

  const isFormFieldValid = (name: string) => !!(formikTouched[name] && formikErrors[name]);
  const getFormErrorMessage = (name: string) => {
    return isFormFieldValid(name) && <small className="p-error">{formikErrors[name]}</small>;
  };

Typescript stops complaining and now is running without error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文