0

I have a React component that takes an array of any type e.g.

type Props = {
  options: any[];
  renderItem: (any, i) => ReactNode
}

How do I edit the types such that the type of the first argument of renderItem is the element type of the options array?

1 Answer 1

1

Make the whole type generic. Something like:

type Props<T extends unknown> = {
  options: T[];
  renderItem: (value: T, i: number) => ReactNode
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can TS derive the type of T automatically or do you have to explicitly mention the type?
I think you have to explicitly note it, but I'm not 100% sure if there might sometimes be a way around it.
I was hoping there'd be a solution where you could pass the options array argument and renderItem would automatically know the type of the options array

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.