I want to implement a generic method that returns a value containing depending on the interface the object has. However cannot figure out how to do it, I've looked at typeof and interfaceof but not sure how to implement it to satisfy below logic.
e.g.
export interface Animal {
name: string;
}
export interface Vehicle {
model: string;
}
getDesignation<T>(data: T): string {
// if data has ANIMAL interface on it
// return data.name;
// if data has VEHICLE interface on it
// return data.model;
}
so example of working code:
const dog: Animal = {
name: 'Rex'
};
const ferrari: Vehicle = {
model: 'Spider'
};
console.log(getDesignation<Vehicle>(ferrari)); /// prints Spider
console.log(getDesignation<Animal>(dog) /// prints Rex