In my TypeScript coding journey, I encountered a challenge in detecting a specific Class type. Despite its seeming simplicity, I found a lack of straightforward documentation on how to accomplish this task. Here is an example that illustrates the issue:
Class Car {}
Class Ford extends Car {}
Class Audi extends Car {}
let mustang = new Ford();
let a6 = new Audi();
// Given instances of Ford and Audi, how can I determine their types dynamically??
console.log(a6.getType()); // <--- looking for 'Audi' here
console.log(mustang.getType()); // <--- expecting 'Ford' as the output
I am exploring possibilities to identify the most specific version of Car (either Audi or Ford) in a generic manner. While acknowledging that "getType()" is not a built-in function, I am curious to know if there exist any simple solutions or alternative perspectives on tackling this problem.