diff --git a/src/query-builder/function-module.ts b/src/query-builder/function-module.ts index e7c582621..a942241c9 100644 --- a/src/query-builder/function-module.ts +++ b/src/query-builder/function-module.ts @@ -160,7 +160,7 @@ export interface FunctionModule { * from "person" * ``` */ - agg>( + agg = ReferenceExpression>( name: string, args?: ReadonlyArray ): AggregateFunctionBuilder diff --git a/test/typings/test-d/aggregate-function.test-d.ts b/test/typings/test-d/aggregate-function.test-d.ts index 9f0ad3bf4..d145fe9f9 100644 --- a/test/typings/test-d/aggregate-function.test-d.ts +++ b/test/typings/test-d/aggregate-function.test-d.ts @@ -109,7 +109,6 @@ async function testSelectExpressionBuilderWithDefaultGenerics( eb.fn.min('age').as('min_age'), eb.fn.sum('age').as('total_age'), ]) - .executeTakeFirstOrThrow() expectAssignable(result.avg_age) @@ -128,6 +127,33 @@ async function testSelectExpressionBuilderWithDefaultGenerics( expectNotAssignable(result.total_age) } +async function testSelectExpressionBuilderWithCustomGenerics( + db: Kysely +) { + const result = await db + .selectFrom('person') + .select((eb) => [ + eb.fn.avg('age').as('avg_age'), + eb.fn.count('age').as('total_people'), + eb.fn.countAll().as('total_all'), + eb.fn.countAll('person').as('total_all_people'), + eb.fn.max('age').as('max_age'), + eb.fn.min('age').as('min_age'), + eb.fn.sum('age').as('total_age'), + eb.fn.agg('max', ['age']).as('another_max_age'), + ]) + .executeTakeFirstOrThrow() + + expectType(result.avg_age) + expectType(result.total_people) + expectType(result.total_all) + expectType(result.total_all_people) + expectType(result.max_age) + expectType(result.min_age) + expectType(result.total_age) + expectType(result.another_max_age) +} + async function testSelectExpressionBuilderWithSubExpressions( db: Kysely ) { @@ -155,7 +181,7 @@ async function testSelectExpressionBuilderWithSubExpressions( } async function testSelectWithCustomGenerics(db: Kysely) { - const { avg, count, countAll, max, min, sum } = db.fn + const { avg, count, countAll, max, min, sum, agg } = db.fn const result = await db .selectFrom('person') @@ -168,6 +194,7 @@ async function testSelectWithCustomGenerics(db: Kysely) { .select(min('age').as('nullable_min_age')) .select(sum('age').as('total_age')) .select(sum('age').as('nullable_total_age')) + .select(agg('max', ['age']).as('max_age')) .executeTakeFirstOrThrow() expectAssignable(result.avg_age) @@ -188,6 +215,7 @@ async function testSelectWithCustomGenerics(db: Kysely) { expectNotAssignable(result.total_age) expectAssignable(result.nullable_total_age) expectNotAssignable(result.nullable_total_age) + expectType(result.max_age) } async function testSelectUnexpectedColumn(db: Kysely) {