In my function, I am working with an array of objects that contain an icon
key. If one index in the array has a value assigned to the icon
key, then another index should also have a value. If one index leaves the icon
key undefined, then another index should also be undefined.
For example:
type Item = {
title: string;
icon?: string | undefined;
};
function Func(items: Array<Item>) {
return items.map((item) => item);
}
Func([
{ icon: 'home', title: 'Home' },
{ icon: 'phone', title: 'Phone' },
{ icon: 'user', title: 'User' },
]); //=> no error
Func([
{ title: 'Home' },
{ title: 'Phone' },
{ title: 'User' }
]); //=> no error
Func([
{ icon: 'home', title: 'Home' },
{ icon: 'phone', title: 'Phone' },
{ title: 'User' },
]); //=> should be error, cause icon not set for `user` but set for another
Func([
{ icon: 'home', title: 'Home' },
{ title: 'Phone' },
{ title: 'User' }
]); //=> should be error, cause icon set for `home` but not set for another