I would like to write generic that is optional (the D in example below) and extends an interface. I can't figure out the syntax.
interface IUpdateItems {
_id: string;
}
export const deleteItem = <T extends IUpdateItems, D = void extends IUpdateItems >(
items: T[],
itemToDelete: T | D
): T[] => {
return items.filter((item) => item._id !== itemToDelete._id);
};
CodePudding user response:
The correct syntax would look like this:
export const deleteItem = <T extends IUpdateItems, D extends IUpdateItems = never>(
items: T[],
itemToDelete: T | D
): T[] => {
return items.filter((item) => item._id !== itemToDelete._id);
};
You should also replace void
with never
since void
is not assignable to IUpdateItems
causing an error.