Currently, I am engaged in a frontend project using Angular for my company. Our main task is to retrieve various objects from an API such as Users, Products, etc.
I am focusing on streamlining our services responsible for transferring data between components and the API to avoid redundancy. My goal is to handle different API calls based on the type of my generic.
In essence, it comes down to this:
public foo<TMyType>(){
if (TMyType === TypeA) {
//do something
} else if (TMyType === TypeB) {
//do something else
} else {
throw new Error('Invalid type');
}
}
It's important to note that I don't intend to pass the actual object, but just make decisions based on the generic type used when calling the function. I believe there should be a simple solution to this challenge, but I haven't been able to come up with one yet.
Thank you very much