英文:
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<'input'> {
...
}
export const Input = React.forwardRef<HTMLInputElement, InputProps>(
(
{
value,
// ^ This is where I get the ESLint error
...rest
},
ref
) => {
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<HTMLInputElement>
) => {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论