I have an interface and two interfaces that extend it:
interface Layer {
name: string;
pretty_name: string;
}
interface SubLayer extends Layer{
url: string;
}
interface AnotherLayer extends Layer{
file: string;
}
Then I a service I have function that handles any Layer argument, and then needs to distinguish between the sub-interfaces and call the correct functions according:
class LayerDataService {
static getLayer(layer: Layer) {
if (......) {
return LayerDataService.getSubLayer(layer as SubLayer );
}
else if (......) {
return LayerDataService.getAnotherLayer(layer as AnotherLayer);
}
}
static getAnotherLayer (layer: AnotherLayer) {
}
static getSubLayer(layer: SubLayer) {
}
}
So on the ..... there I want to distinguish between layers which implement SubLayer and layers implementing AnotherLayer.
So I know I can't use instanceof, because they aren't classes, they're objects implementing interfaces. But is there a way that doesn't involve manually checking each-and-every attribute manually like I can do in type guards?