Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tuples #611

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
improve typings
  • Loading branch information
koskimas committed Jul 25, 2023
commit 1f6ea12f3ceb5e3ebc597d8f3b7e130ead54c071
38 changes: 25 additions & 13 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1459,42 1459,54 @@ export interface SelectQueryBuilder<DB, TB extends keyof DB, O>
* )
* ```
*/
$asTuple<K1 extends keyof O, K2 extends keyof O>(
$asTuple<K1 extends keyof O, K2 extends Exclude<keyof O, K1>>(
key1: K1,
key2: K2
): ExpressionWrapper<DB, TB, [O[K1], O[K2]]>
): keyof O extends K1 | K2
? ExpressionWrapper<DB, TB, [O[K1], O[K2]]>
: KyselyTypeError<'$asTuple() call failed: All selected columns must be provided as arguments'>

$asTuple<K1 extends keyof O, K2 extends keyof O, K3 extends keyof O>(
$asTuple<
K1 extends keyof O,
K2 extends Exclude<keyof O, K1>,
K3 extends Exclude<keyof O, K1 | K2>
>(
key1: K1,
key2: K2,
key3: K3
): ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3]]>
): keyof O extends K1 | K2 | K3
? ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3]]>
: KyselyTypeError<'$asTuple() call failed: All selected columns must be provided as arguments'>

$asTuple<
K1 extends keyof O,
K2 extends keyof O,
K3 extends keyof O,
K4 extends keyof O
K2 extends Exclude<keyof O, K1>,
K3 extends Exclude<keyof O, K1 | K2>,
K4 extends Exclude<keyof O, K1 | K2 | K3>
>(
key1: K1,
key2: K2,
key3: K3,
key4: K4
): ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3], O[K4]]>
): keyof O extends K1 | K2 | K3 | K4
? ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3], O[K4]]>
: KyselyTypeError<'$asTuple() call failed: All selected columns must be provided as arguments'>

$asTuple<
K1 extends keyof O,
K2 extends keyof O,
K3 extends keyof O,
K4 extends keyof O,
K5 extends keyof O
K2 extends Exclude<keyof O, K1>,
K3 extends Exclude<keyof O, K1 | K2>,
K4 extends Exclude<keyof O, K1 | K2 | K3>,
K5 extends Exclude<keyof O, K1 | K2 | K3 | K4>
>(
key1: K1,
key2: K2,
key3: K3,
key4: K4,
key5: K5
): ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3], O[K4], O[K5]]>
): keyof O extends K1 | K2 | K3 | K4 | K5
? ExpressionWrapper<DB, TB, [O[K1], O[K2], O[K3], O[K4], O[K5]]>
: KyselyTypeError<'$asTuple() call failed: All selected columns must be provided as arguments'>

/**
* Narrows (parts of) the output type of the query.
Expand Down
18 changes: 18 additions & 0 deletions test/typings/test-d/expression.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 267,22 @@ function testExpressionBuilderTuple(db: Kysely<Database>) {
])
)
)

// Not all selected columns provided for $asTuple
expectType<
KyselyTypeError<'$asTuple() call failed: All selected columns must be provided as arguments'>
>(
db
.selectFrom('person')
.select(['first_name', 'last_name', 'age'])
.$asTuple('first_name', 'last_name')
)

// Duplicate column provided for $asTuple
expectError(
db
.selectFrom('person')
.select(['first_name', 'last_name'])
.$asTuple('first_name', 'last_name', 'last_name')
)
}
Loading