I am trying to create a generic component which can expect data of two types only:
interface X{
name: string,
path: string,
type: string,
}
interface Y{
name: string,
path: string,
}
Both types X,Y have two common properties and X has one extra. Now I am defining the type this way:
export class MyComponent<T extends X | Y> implements OnInit{
@Input() data: T[];
func(item: T){
let temp = this.data.find(x => x.name === item.name);
<<....some code....>>
}
}
Calling this from the parent component this way:
<my-component [data]="xList"></my-component> <!-- xList: X[] -->
<my-component [data]="yList"></my-component> <!-- yList: y[] -->
This is working fine but I am not sure if this <T extends X | Y> is the correct way to do this or not. Can anybody please suggest the best approach here?
Can we write something like where T implements X | Y in Typescript?
Or should I just use @Input() data: any[];?