Skip to content

Commit

Permalink
Bring back default for second agg generic. Fixes #829
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Jan 2, 2024
1 parent d926c89 commit e762244
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* from "person"
* ```
*/
agg<O, RE extends ReferenceExpression<DB, TB>>(
agg<O, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
name: string,
args?: ReadonlyArray<RE>
): AggregateFunctionBuilder<DB, TB, O>
Expand Down
32 changes: 30 additions & 2 deletions test/typings/test-d/aggregate-function.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ async function testSelectExpressionBuilderWithDefaultGenerics(
eb.fn.min('age').as('min_age'),
eb.fn.sum('age').as('total_age'),
])

.executeTakeFirstOrThrow()

expectAssignable<string | number>(result.avg_age)
Expand All @@ -128,6 +127,33 @@ async function testSelectExpressionBuilderWithDefaultGenerics(
expectNotAssignable<null>(result.total_age)
}

async function testSelectExpressionBuilderWithCustomGenerics(
db: Kysely<Database>
) {
const result = await db
.selectFrom('person')
.select((eb) => [
eb.fn.avg<number>('age').as('avg_age'),
eb.fn.count<number>('age').as('total_people'),
eb.fn.countAll<number>().as('total_all'),
eb.fn.countAll<number>('person').as('total_all_people'),
eb.fn.max<number>('age').as('max_age'),
eb.fn.min<number>('age').as('min_age'),
eb.fn.sum<number>('age').as('total_age'),
eb.fn.agg<number>('max', ['age']).as('another_max_age'),
])
.executeTakeFirstOrThrow()

expectType<number>(result.avg_age)
expectType<number>(result.total_people)
expectType<number>(result.total_all)
expectType<number>(result.total_all_people)
expectType<number>(result.max_age)
expectType<number>(result.min_age)
expectType<number>(result.total_age)
expectType<number>(result.another_max_age)
}

async function testSelectExpressionBuilderWithSubExpressions(
db: Kysely<Database>
) {
Expand Down Expand Up @@ -155,7 +181,7 @@ async function testSelectExpressionBuilderWithSubExpressions(
}

async function testSelectWithCustomGenerics(db: Kysely<Database>) {
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')
Expand All @@ -168,6 +194,7 @@ async function testSelectWithCustomGenerics(db: Kysely<Database>) {
.select(min<number | null>('age').as('nullable_min_age'))
.select(sum<number>('age').as('total_age'))
.select(sum<number | null>('age').as('nullable_total_age'))
.select(agg<number>('max', ['age']).as('max_age'))
.executeTakeFirstOrThrow()

expectAssignable<number>(result.avg_age)
Expand All @@ -188,6 +215,7 @@ async function testSelectWithCustomGenerics(db: Kysely<Database>) {
expectNotAssignable<string | bigint | null>(result.total_age)
expectAssignable<number | null>(result.nullable_total_age)
expectNotAssignable<string | bigint>(result.nullable_total_age)
expectType<number>(result.max_age)
}

async function testSelectUnexpectedColumn(db: Kysely<Database>) {
Expand Down

1 comment on commit e762244

@vercel
Copy link

@vercel vercel bot commented on e762244 Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kysely – ./

kysely-kysely-team.vercel.app
www.kysely.dev
kysely.dev
kysely-git-master-kysely-team.vercel.app

Please sign in to comment.