I am working with three different interfaces:
X.ts
export interface X {
id: number;
name: string;
dateCreated: string;
info: string
}
Y.ts
export interface Y {
id: number;
name: string;
dateCreated: string;
category: string;
email: string;
}
XY.ts
export interface XY extends X, Y {
type: string;
assignedTo: string;
}
Presently, I am utilizing these interfaces within a generic function for sorting purposes:
function sort<T extends XY | X | Y>(items: T[], keyName: string) {
switch keyName {
case "id": {
items.sort((a, b) => a.id > b.id ? 1 : -1);
} case "type": {
items.sort((a, b) => a.type > b.type ? 1 : -1);
break;
} case "info": {
items.sort((a, b) => a.info > b.info ? 1 : -1);
break;
} case "category": {
items.sort((a, b) => a.category > b.category ? 1 : -1);
break;
}
}
}
However, an error message is being displayed as follows:
Property 'type' does not exist on type 'XY | X | Y'.
Given that items
is of type T
, which is a generic (extending from XY
, X
, and Y
), I expect it to have the type
attribute and not prompt this error. Is there a mistake in my understanding?