Correct type definitions for function receiving builder

Dddanielcruzz5/10/2023
I have something like this and I was wondering if it's the correct way to type my helper function.

// ----- Helper function I want to type
const addCategoriesWhere = (
  sqb: SelectQueryBuilder<DB, "Collection", {}>,
  categories: CategoryValues[]
) => {
  if (categories.includes("ALL")) {
    return sqb;
  }

  const categoriesWithoutAll = categories.filter(
    (c) => c !== "ALL"
  ) as RemoveElementFromArray<CategoryValues[], "ALL">;

  return sqb.where("categories", "in", categoriesWithoutAll);
};

// ------ Use of helper
const base = db
      .selectFrom("Collection")
      .where("is_og", "=", true)

const withCategoryWhere = addCategoriesWhere(base, categories);

I'm getting a typescrip eslint error, Idk if the third generic argument should be different or I can ignore eslint
Kkoskimas5/11/2023
Something like this:

const addCategoriesWhere = <O>(
  sqb: SelectQueryBuilder<DB, "Collection", O>,
  categories: CategoryValues[]
) => {
  if (categories.includes("ALL")) {
    return sqb;
  }

  const categoriesWithoutAll = categories.filter(
    (c) => c !== "ALL"
  ) as RemoveElementFromArray<CategoryValues[], "ALL">;

  return sqb.where("categories", "in", categoriesWithoutAll);
};
Dddanielcruzz5/18/2023
Thanks