Home > Enterprise >  How to use 2 interfaces to describe any array that return 2 different types of results
How to use 2 interfaces to describe any array that return 2 different types of results

Time:11-26

I have this code down below I want instead of using any I want to describe the 2 different values using the interface the problem here is it is not working can I use 2 interfaces as union type? if not how to describe the output of this (set) variable then?

interface Arr {
  [index: string]: [
    {
      id: string;
      name: string;
    }
  ];
}

interface Obj {
  [index: string]: {
    [index: string]: [
      {
        id: string;
        name: string;
      }
    ];
  };
}


  const set : Arr | Obj = { ItemsArr: context.ItemsArr['ItmesArr'], ItemsObj: context.ItemsObj['ItmesObj'] };
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

interface ItemInterface {
  ItemsArr : Arr,
  ItemsObj : Obj
}

interface Arr {
  [index: string]: [
    {
      id: string;
      name: string;
    }
  ];
}

interface Obj {
  [index: string]: {
    [index: string]: [
      {
        id: string;
        name: string;
      }
    ];
  };
}


  const set : ItemInterface = { ItemsArr: context?.ItemsArr['ItmesArr'], ItemsObj: context?.ItemsObj['ItmesObj'] };
  • Related