Correct type definitions for function receiving builder

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);
// ----- 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
2 Replies
koskimas
koskimas13mo ago
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);
};
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);
};
Daniel Cruz
Daniel Cruz13mo ago
Thanks
Want results from more Discord servers?
Add your server
More Posts