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
SlayMaster3000•

It doesn't look like you are restricting T in anyway. Declare T as something like this:

T extends { id: number; list: unknown[]; }

2
gloriousReadOP•

Let me try that

1
serhii_2019•

You need to pass T to IProps interface, because currently T is unused. See example here

2