If I have a component,
import { FC } from 'react';
interface Props {
name: string;
}
const Home: FC<Props> = ({ name, ...rest }) => {
return (
<>
<div>{name}</div>
<input {...rest} type="text" />
</>
);
};
export default Home;
and try to use it like <Home name="testName" placeholder="test" />, my editor is mad giving red underline under placeholder.
Raw Error:
Type '{ name: string; placeholder: string; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
Property 'placeholder' does not exist on type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
I tried extending the Props with HTMLAttributes but it didn't work.
interface Props extends HTMLAttributes<HTMLElement> {
name: string;
}
CodePudding user response:
This has nothing to do with the rest operator.
interface Props { name: string; }
Your interface doesn't allow a placeholder attribute on the element.
If you want to pass through properties allowed on an input, then you have to include those properties on the interface.
interface Props extends React.ComponentPropsWithoutRef<"input"> {
name: string;
}
CodePudding user response:
- What is FC?
It's a React functional component definition, which by default haschildren?: ReactNodeinterface defined which is generic and can be extended.\ - You are trying to define
propsfor your component.
In your use case You providedconst Home: FC<Props>. This means, You've extended the interface ofReact.FCwith yourPropsdefinition. This means that you have
{children: ReactNode, name: string}described to typescript as valid parameters that you are expecting.
This means if you pass something else, that is not defined in the interface, You will get an error.
In Your case You are destructuring the parameters:
name,...rest. From Your interface definition this means, that...restwill be only the property -children, because that's what You defined in your interface.\ - While the simplest solution to solve Your error would be only to introduce a new property in your interface called placeholder, a better solution would be to provide an interface, that would match your
...restspread operator. Since all of Your props go inside of button, you can extend your Props interface withReact.ButtonHTMLAttributes<HTMLButtonElement>.
interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> {
name: string;
}
const Home: FC<Props> = ({ name, ...rest }) => {
return (
<>
<div>{name}</div>
<input {...rest} type="text" />
</>
);
};
