ESLint报错:在props验证中缺少prop,但实际已存在。

huangapple go评论56阅读模式
英文:

ESLint `prop is missing in props validation` but it's there

问题

Here is the translation of the provided content:

我正在构建一个 Input,其属性派生自 HTML Input 的属性。一切都应该是类型安全的,并且IDE应该识别出 rest 包含所有HTML输入属性(尽管我没有明确编写它们 - 这要归功于继承)。然而,ESLint表示它们没有被验证。

interface InputProps extends React.ComponentPropsWithoutRef<'input'> {
  ...
}

export const Input = React.forwardRef<HTMLInputElement, InputProps>(
  (
    {
      value,
      // ^ 这是我得到ESLint错误的地方
      ...rest
    },
    ref
  ) => {

在这里应该做什么是正确的事情?

英文:

I am building an Input whose props are derived from the HTML Input props. Everything should be type safe, and the IDE recognizes that rest contains all the HTML input props (even though I did not explicitly write them - thanks to the inheritance).
However, ESLint says they're not validated.

interface InputProps extends React.ComponentPropsWithoutRef&lt;&#39;input&#39;&gt; {
  ...
}

export const Input = React.forwardRef&lt;HTMLInputElement, InputProps&gt;(
  (
    {
      value,
      // ^ This is where I get the ESLint error
      ...rest
    },
    ref
  ) =&gt; {

What's the correct thing to do here?

答案1

得分: 0

If we remove the generic types passed to forwardRef and explicitly set the types for the function args, that solves the problem:

export const Input = React.forwardRef(
  (
    {
      value
    }: InputProps,
    ref: ForwardedRef<HTMLInputElement>
  ) => {
英文:

If we remove the generic types passed to forwardRef and explicitly set the types for the function args, that solves the problem:

export const Input = React.forwardRef(
  (
    {
      value
    }: InputProps,
    ref: ForwardedRef&lt;HTMLInputElement&gt;
  ) =&gt; {

huangapple
  • 本文由 发表于 2023年5月7日 22:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定