Looking to iterate through an object that combines two interfaces.
interface Family {
cat: string;
age: string;
family: string;
lastYearFamily: string;
}
interface Model {
cat: string;
age: string;
model: string;
lastYearModel: string;
}
interface Props {
attributionType: 'family' | 'model';
attributions?: Family[] | Model[];
}
const RowComponent = ({
attributionType,
attributions
}: props) =>{
return (
{attributions && attributions.map((attribution: Family | Model ) => (
<tr>
{
attributionType === 'family' && (
<React.Fragment>
<th>{attribution.family}</th>
<th>Family</th>
</React.Fragment>
)
}
{
attributionType === 'model' && (
<React.Fragment>
<th>{attribution.model}</th>
<th>Model</th>
</React.Fragment>
)
}
</tr>
))}
);
}
Struggling with accessing non-common members in the union object.
Can only access cat
and age
, not family
, lastYearFamily
, etc.
Prefer to keep code generic without separate components for each attribution type.