I'm currently in the process of developing a versatile component that can handle data of only two specific types:
interface X{
name: string,
path: string,
type: string,
}
interface Y{
name: string,
path: string,
}
Both types X and Y share two common properties, with X having an extra one. At this point, I've defined the type as follows:
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....>>
}
}
When calling this from the parent component, it looks like this:
<my-component [data]="xList"></my-component> <!-- xList: X[] -->
<my-component [data]="yList"></my-component> <!-- yList: y[] -->
Everything seems to be functioning correctly, but I'm unsure if <T extends X | Y>
is the most suitable method for achieving this. Can anyone offer suggestions on the best approach?
Is it possible to declare something like where T implements X | Y
in Typescript?
Alternatively, should I simply use @Input() data: any[];
instead?