0

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;
}

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1
  1. What is FC?
    It's a React functional component definition, which by default has children?: ReactNode interface defined which can be extended.
  2. You are trying to define props for your component.
    In your use case You provided const Home: FC<Props>. This means, You've extended the interface of React.FC with your Props definition. 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 ...rest will be only the property - children , because that's what You defined in your interface.\
  3. 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 ...rest spread operator. Since all of Your props go inside of button, you can extend your Props interface with React.ButtonHTMLAttributes<HTMLButtonElement>.
interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  name: string;
}

const Home: FC<Props> = ({ name, ...rest }) => {
  return (
    <>
      <div>{name}</div>
      <input {...rest} type="text" />
    </>
  );
};

Update: A VSCODE trick on inspecting what types does an element, or its property has is to simply hover on it. enter image description here

2 Comments

Thanks, I didn't know about the types React comes with inbuilt. Do you know any site where common React inbuilt types are listed?
I think there is a site. But it's not much help. What You can do is use your IDE for that.
0

This article offers a nice set of options for solving the need to pass an unknown set of arguments to a React component that returns an HTML input element with a spread of those arguments: https://mortenbarklund.com/blog/react-typescript-props-spread/

Credit to Morton Barkland for the very comprehensive explanation. Re-posting here because I was unable to find a comparable solution on StackOverflow.

import type { ComponentPropsWithoutRef } from "react";

interface InputGroupProps extends ComponentPropsWithoutRef<"input"> {
  label: string;
}

function InputGroup({ label, ...rest }: InputGroupProps) {
  return (
    <label>
      {label}:
      <input {...rest} />
    </label>
  );
}

And then, the usage of the component in the component:

function App() {
  return (
    <form>
      <h1>Signup</h1>
      <InputGroup label="Email" type="email" placeholder="E.g. [email protected]" />
      <InputGroup label="Password" type="password" minLength={8} />
      <button>Submit</button>
    </form>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.