Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
12 replies
Xaohs

How to check if type is array?

I have the following interface
interface IFilterOptions {
  price: {
    min: number,
    max: number,
  },
  item_type: string[],
  state: string[],

  [key: string]: string[] | {min: number, max: number};
}


How can I check if i'm dealing with the object type or the string[] type while mapping through the key's of IFilterOptions
ie:

Object.keys(filterOptions).forEach((filter) => {
    if (filter === "price") {
          ....
          return;
      } else if (Array.isArray(filterOptions[filter]) && !filterOptions[filter].includes("test")) 
        ❌❌ -> Property 'includes' does not exist on type 'string[] | { min: number; max: number; }' ❌❌
      }
});
Was this page helpful?