Skip to content
Advertisement

Add type for onClick property which is in …props in React TypeScript

I have a button component in which I’m trying to write the prop types using type and I see this error in the console. Could anyone please help?

Type '{ children: string; label: string; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes & Props'.
  Property 'onClick' does not exist on type 'IntrinsicAttributes & Props'.  TS2322

Excerpt from my code

type Props = {
  label: string;
  children: ReactNode;
};

const Button = ({ label, children, ...props }: Props) => (
  <button label={label} {...props}>
    {children}
  </button>
);

Advertisement

Answer

You need to add onClick into Props like:

type Props = {
  label: string;
  children: ReactNode;
  onClick: () => void;
};
User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement