简体   繁体   English

Typescript 错误和扩展运算符由……道具引起?

[英]Typescript Error and the spread operator arising from …props?

Hi I am running into issues when I am trying to use the spread operator with Typescript.嗨,当我尝试将扩展运算符与 Typescript 一起使用时,我遇到了问题。

I have the function where the useFormikContext() and useField() functions are from Formik's library.我有 function 其中useFormikContext()useField()函数来自 Formik 的库。 When I add the //@ts-ignore everything works superbly as intended however, without that line I receive an error:当我添加//@ts-ignore时,一切都按预期工作,但是,如果没有该行,我会收到一个错误:

const DatePickerField = ({ ...props }) => {
  const { setFieldValue } = useFormikContext();
  //@ts-ignore
  const [field] = useField(props);
  return (
    <DatePicker
      {...field}
      {...props}
      selected={(field.value && new Date(field.value)) || null}
      onChange={(val) => {
        setFieldValue(field.name, val);
      }}
    />
  );
};
Argument of type '{ [x: string]: any; }' is not assignable to parameter of type 'string | (ClassAttributes<HTMLInputElement> & InputHTMLAttributes<HTMLInputElement> & FieldConfig<any>) | (ClassAttributes<...> & ... 1 more ... & FieldConfig<...>) | (ClassAttributes<...> & ... 1 more ... & FieldConfig<...>)'.
  Type '{ [x: string]: any; }' is not assignable to type 'ClassAttributes<HTMLInputElement> & InputHTMLAttributes<HTMLInputElement> & FieldConfig<any>'.
    Property 'name' is missing in type '{ [x: string]: any; }' but required in type 'FieldConfig<any>'.  TS2345

From what I understand this function accepts a spread operator, so that is meant to say it accepts an object / array with 0 or more properties.据我了解,此 function 接受扩展运算符,这意味着它接受具有 0 个或多个属性的 object / 数组。 it automatically got assigned the type { [x: string]: any; }它自动被分配了类型{ [x: string]: any; } { [x: string]: any; } which is meant to say that there is a key (x) with a value type of any. { [x: string]: any; }这意味着有一个值类型为 any 的键 (x)。 However, I don't know how to fix this error given by typescript.但是,我不知道如何修复 typescript 给出的这个错误。

Okay after some researching here is what I found to fix the errors.好的,经过一番研究,我发现可以修复这些错误。

The syntax '{ [x: string]: any;语法 '{ [x: string]: any; }' is letting me know that this object has a key of x which is a string with a value of any. }' 让我知道这个 object 的键 x 是一个值为 any 的字符串。 I didn't know this at the time of the question I was confused a little bit.在提出问题时我不知道这一点,我有点困惑。 https://www.typescriptlang.org/docs/handbook/advanced-types.html <-- index types and index signatures https://www.typescriptlang.org/docs/handbook/advanced-types.html <--索引类型和索引签名

Typescript was also letting me know that the useField() function required the property name of value string. Typescript 还让我知道useField() function 需要值字符串的属性名称。 so I typed it using this newfound knowledge and got rid of an unused function selected and the new code now looks like this:所以我使用这个新发现的知识输入它并删除了一个未使用的 function selected ,新代码现在看起来像这样:

const DatePickerField = ({ ...props }: { [x: string]: any; name: string }) => {
  const { setFieldValue } = useFormikContext();

  const [field] = useField(props);
  return (
    <DatePicker
      {...field}
      {...props}
      // selected={(field.value && new Date(field.value)) || null}
      onChange={(val) => {
        setFieldValue(field.name, val);
      }}
    />
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM