enhance type safety for enum predicates

pull/28686/head
timonrieger 2026-06-02 17:54:17 +02:00
parent 37b5d68be2
commit 35c850ddbc
No known key found for this signature in database
1 changed files with 10 additions and 4 deletions

View File

@ -36,6 +36,7 @@ import {
AssetFileType,
AssetOrder,
AssetOrderBy,
AssetType,
AssetVisibility,
DatabaseExtension,
ExifOrientation,
@ -717,12 +718,17 @@ function idPredicates(
return predicates;
}
function enumPredicates<T extends string>(
type EnumColumn = {
'asset.type': AssetType;
'asset.visibility': AssetVisibility;
};
function enumPredicates<C extends keyof EnumColumn>(
eb: AssetExpressionBuilder,
column: 'asset.type' | 'asset.visibility',
filter: { eq?: T; ne?: T; in?: T[]; notIn?: T[] } = {},
column: C,
filter: { eq?: EnumColumn[C]; ne?: EnumColumn[C]; in?: EnumColumn[C][]; notIn?: EnumColumn[C][] } = {},
) {
// `as never`: kysely's column-union type can't narrow to T; SearchFilter enum schemas validate at the boundary.
// casts: kysely's `eb` doesn't distribute its column-value narrowing through the generic
const predicates: Expression<SqlBool>[] = [];
if (filter.eq !== undefined) {
predicates.push(eb(column, '=', filter.eq as never));