Mapping inner array of generics, "Property 'list' does not exist on type 'T'."
Basically I want to access the list array however I got issue on it.
const data = [
{
id: 1,
list: []
},
{
id: 2,
list: []
}
]
// component A - Parent
const Parent = () => <Child entry={entry} />
// component B - Child
interface IProps<T> { entry: T[] }
const Child = <T,>({ entry}: IProps) => {
return (
<>
// I want to access the list []
// However got error Property 'list' does not exist on type 'T'.
{entry.map((item) => item.list)}
</>
)
}
Posted Feb 9, 2022, 3:32 AM UTC · edited
23
It doesn't look like you are restricting T in anyway. Declare T as something like this:
T extends { id: number; list: unknown[]; }