Home > Back-end >  A type guard on literal union types
A type guard on literal union types

Time:01-23

I have two types in TypeScript:

type PublicMethods = 'Time' | 'Assets' | 'AssetPairs' ;
type PrivateMethods = 'Balance' | 'TradeBalance';

I would like to use the same api function to handle these types, but the behaviour is different for each. Something along the lines of:

public api = (method: PublicMethods | PrivateMethods, params: any) => {

  // ...how do I create a type guard here?
  if(method instanceof PublicMethods) { //            
  • Related