K
Join ServerKysely
help
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.
I'm getting a typescrip eslint error, Idk if the third generic argument should be different or I can ignore eslint
// ----- 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
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);
};
Thanks