I have this item interface:
interface Item<Href extends string> {
href: Route<Href>
}
I want to have a component that takes a list of these Item
in a single property:
interface Props {
items: Item[]
}
const Component = (props: Props): ReactNode => { ... };
<Component items={[
{ href: '/' },
{ href: '/auth' },
]} />
How can I make this work? Currently it complains because I’m not passing the generic to the Item
interface when I define Props
.
2
Answers
Thanks to @Konrad's comment, the solution was simpler than I expected.
It was enough to add a generic to the component type
Here's a full example:
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcA5FDvmQNwBQokscMAnmFnAN6ZUwByEACacAvnCIlylPDFp06bDnADKMZDGC4MEAK4ws6ALzkA9GTgAfM8n0ALMovacd+rAB4AKnCwAPAwB2QuioMFDAAQDmAHxwJt5+gcGq6praegboAPxw3gBccAFYAG5YUPQMAQZQBHicAJIGIO4AEpQEPv5YQSFhETHcdHBwdu0Frgat7dF0ogoR1bW4nAAKxGCoXp1JveFRsVxDcMBNqAWNWM2e0QDaALqzCrgQAaFwAMIkkEVVcXBbiW6yVCe0iABpogAKMDrM5wNYQDZeaIASnGfEEIjiBzgR0oMF0UAChV0ABtSfQ4KIKs9XvAAIJgMB-SEo7H-T7gF7deAnS6oIxcG5HHijLAEApkcxUsEikZjGz2CyiWV3cSmaJAA
I believe that you should specify the type as well, you are defining in your item to get a
Href
that extends fromstring
, so you can do the same in the Props interface or setstring
directly.I made this quick example:
It is a good practice to use Array instead of Type[] or [Type], it is easier to read.