astahmer / typed-openapi

Generate a headless Typescript API client from an OpenAPI spec
https://typed-openapi-web.vercel.app/
200 stars 23 forks source link

anyOf is wrongly creating a union with the array of T #31

Open alykamb opened 7 months ago

alykamb commented 7 months ago

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);