1

In Tab.tsx:

import React from "react";

interface Props {
  title: string;
  index: number;
  onClick?: React.MouseEventHandler<HTMLElement>;
}

const Tab: React.FC<Props> = ({ title, onClick, index }) => {
  return (
    <li>
      <button onClick={onClick}>
        {title}
      </button>
    </li>
  );
};

export default Tab;

Trying to use it with a named function

const changeTab = (e: MouseEvent<HTMLButtonElement>) => {
  console.log(e);
};

return <Tab onClick={changeTab}></Tab>

I have tried all different combinations of the types and can not get it to work currently receiving following error:

'{ onClick: (e: MouseEvent<HTMLButtonElement, MouseEvent>) => void; }' is missing the following properties from type 'Props': title, index, active

1 Answer 1

2

It's complaining because you stated that props would include two other items (title and index), but they aren't present.

You either need to make them optional:

interface Props {
  title?: string;
  index?: number;
  onClick?: React.MouseEventHandler<HTMLElement>;
}

Or add the properties:

return <Tab onClick={changeTab} title={''} index={0}></Tab>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you are right. New to type script and the errors are sometimes a little tricky to understand what it is complaining about!

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.