0
type List<T> = {
  items: T[];
  clickHandler: (a: T) => void;
};

const List = <T extends {}>({
  items,
  clickHandler,
}: List<T>) => {
  return (
    <div>
      {items.map((item, index) => (
        <div key={index} onClick={() => clickHandler(item)}>
          {item} // error is showing here
        </div>
      ))}
    </div>
  );
};

export default List;

typescript Error Type 'T' is not assignable to type 'ReactNode'. Type '{}' is not assignable to type 'ReactNode'. Type 'T' is not assignable to type 'ReactPortal'. Type '{}' is missing the following properties from type 'ReactPortal': key, children, type, propsts(2322)

0

2 Answers 2

4

It seems you want to render the items of type T. You have to ensure react that T is "renderable type".

One way to do this is:

const List = <T extends ReactNode>

More info here

Sign up to request clarification or add additional context in comments.

Comments

1

Since you're rendering items as children of div you can also define it as

const List = <T extends JSX.IntrinsicElements['div']['children']>(

which internally is defined as React.ReactNode would result in the same as @Svetoslav Retkov's answer

TS Playground

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.