In TypeScript, I have defined two types:
type PublicMethods = 'Time' | 'Assets' | 'AssetPairs';
type PrivateMethods = 'Balance' | 'TradeBalance';
I am looking to streamline the usage of the api
function for both types, but with different behaviors for each. Here is what I have tried:
public api = (method: PublicMethods | PrivateMethods, params: any) => {
// How can I implement a type guard here?
if(method instanceof PublicMethods) { // 😱 This doesn't work!
// ...
}
}
I also attempted overloading the function:
public api(method: PublicMethods | PrivateMethods, params: any, callback: Function);
public api(method: PublicMethods, params: any, callback: Function) {
// ...implementation
}
public api(method: PrivateMethods, params: any, callback: Function) {
// ...implementation
}
Neither approach worked as expected. Any ideas on how to achieve this?