【问题标题】:typescript has a problem with react-hook-form fieldArray of numbers field nametypescript 对 react-hook-form fieldArray of numbers 字段名有问题
【发布时间】:2022-12-16 02:37:40
【问题描述】:

标题很长,但本质上在 react-hook-form 中的 fieldArray 定义上有一个 Type 'string' is not assignable to type 'never'.ts(2322) fieldArray.d.ts(7, 5): The expected type comes from property 'name' which is declared here on type 'UseFieldArrayProps<FormValues, never, "id">' 错误,它有时会消失,但大多数时候都存在,我不知道为什么会存在,因为所有示例都这样显示它有时没有任何变化就消失了。有谁知道问题出在哪里?为什么打字稿抱怨?

我试过更改版本,重新排序控件和名称值(它删除了一次错误,当我再次交换它们时它又回来了,无论我再次交换它们多少次它都留在那里)。

这是其中一个错误,我一点也不知道它是从哪里来的。

Codesandbox 链接在这里:https://codesandbox.io/s/react-hook-form-list-of-numbers-s6zg2p?file=%2Fsrc%2FApp.tsx

编辑:错误特别出现在第 35 行。

【问题讨论】:

    标签: reactjs typescript react-hook-form


    【解决方案1】:

    你只需要更新 react-hook-form 的版本。我检查了上面的代码并且在更新版本时它工作正常。后来也在这里修好了 https://github.com/react-hook-form/react-hook-form/blob/4f52102e52434ab81385856bbf81561b3253c3cb/src/types/fieldArray.ts

    【讨论】:

    • 你说的是什么版本? “最新的”7.40.0 会导致许多其他类型错误,大多数内容都被键入了 any。这就是我回到以前使用过的版本并且没有遇到那个问题的原因。直到我遇到这篇文章是关于的。编辑:这里是指向那个版本的叉子的链接codesandbox.io/s/…
    【解决方案2】:

    这是因为 react-hook-form 仅支持对象类型作为 useFieldArray 挂钩中的值。例如,您不能将它与 string[] 或 number[] 一起使用。

    问题在这里讨论:https://github.com/react-hook-form/react-hook-form/discussions/7586

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以在in the help center找到更多关于如何写出好的答案的信息。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2022-07-30
    • 2022-11-02
    • 2020-05-09
    • 2021-04-09
    相关资源
    最近更新 更多