I have a question regarding how to best approach a generic component that can be reused. I have tried to simplify the case as much as I can.
Say we create an angular component custom-list
, that takes an input of ListItem[]
.
The HTML, very simple, can be something like:
<div>
<ul>
<li
*ngFor="let item of this.itemList"
(click)="this.itemClicked(item)"
>
{{ item.title }}
</li>
</ul>
</div>
Now, I have a question regarding the type. I define ListItem
as follows:
export interface ListItem {
title: string;
innerElement: any;
}
This allows me to create custom lists from objects of any types and save the object as a reference if I wish to access it later. Say that:
interface Mailbox {
mailboxName : string
mailboxEmails: number
}
const mailboxes : Mailbox[] = [
{
mailboxName: "Mailbox one",
mailboxEmails: 43,
},
{
mailboxName: "Mailbox two",
mailboxEmails: 3
}
]
this.itemList: ListItem[] = mailboxes.map(x => ({title: x.mailboxName, innerElement: x}));
Now, the problem here, is that I lose the type Mailbox
when I try to access the items in list
, due to innerElement
being of the type any
. I'd really like to keep that. How would I go about designing such a pattern?