typed-openapi icon indicating copy to clipboard operation
typed-openapi copied to clipboard

anyOf is wrongly creating a union with the array of T

Open alykamb opened this issue 11 months ago • 0 comments

When we have a union of multiple objects, all of them have a type property, we cannot check the type property because of the union with an array. And the value itself will never be an array, it just needs to be checked with each possible value:

example:

  export type RangeFilter = {
    type: "filter_range";
    id: string;
    displayName: string;
    min: number;
    max: number;
    step?: number | undefined;
    minSelected?: number | undefined;
    maxSelected?: number | undefined;
  };
  export type SelectFilterValue = {
    label: string;
    value: string;
    selected?: boolean | undefined;
    productCount?: number | undefined;
  };
  export type SelectFilter = { id: string; displayName: string; values: Array<SelectFilterValue> };
  export type SingleSelectFilter = { type: "filter_single_select" } & SelectFilter;
  export type MultiSelectFilter = { type: "filter_multi_select" } & SelectFilter;
  
  export type Filters =
    | RangeFilter
    | SingleSelectFilter
    | MultiSelectFilter
    | Array<RangeFilter | SingleSelectFilter | MultiSelectFilter>; // wrong, it will never be an array, it can only be one of the objects, or something that matches multiple objects at the same time.

Although the generation part might be complicated and blow up too fast, maybe I'm going over the top, but I think the correct type needs to address every possible combination:

 export type Filters =
    | RangeFilter
    | SingleSelectFilter
    | MultiSelectFilter
    | (RangeFilter & SingleSelectFilter)
    | (SingleSelectFilter & MultiSelectFilter)
    | (RangeFilter & MultiSelectFilter)
    | (RangeFilter & SingleSelectFilter & MultiSelectFilter);

alykamb avatar Mar 19 '24 09:03 alykamb