Skip to content

Commit 2df2d43

Browse files
committed
add type test file stub for generic stuff
1 parent 41cbc8b commit 2df2d43

File tree

2 files changed

+107
-31
lines changed

2 files changed

+107
-31
lines changed

test/typings/test-d/generic.test-d.ts

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { Kysely, ExpressionBuilder, SelectQueryBuilder, Generated } from '..'
2+
3+
import { expectAssignable, expectType } from 'tsd'
4+
import { Database } from '../shared'
5+
6+
// TODO: type-checking this is crazy slow. Figure out the cause.
7+
function testSelectQueryBuilderExtends() {
8+
type A = { a: number }
9+
type B = { b: string }
10+
11+
type T1 = SelectQueryBuilder<{ a: A }, 'a', unknown>
12+
13+
// This type extends T1 and should be assignable to it.
14+
type T2 = SelectQueryBuilder<{ a: A; b: B }, 'a' | 'b', { a: number }>
15+
16+
const t2 = {} as T2
17+
expectAssignable<T1>(t2)
18+
}
19+
20+
// TODO: type-checking this is crazy slow. Figure out the cause.
21+
function testExpressionBuilderExtends() {
22+
type A = { a: number }
23+
type B = { b: string }
24+
25+
type T1 = ExpressionBuilder<{ a: A }, 'a'>
26+
27+
// This type extends T1 and should be assignable to it.
28+
type T2 = ExpressionBuilder<{ a: A; b: B }, 'a' | 'b'>
29+
30+
const t2 = {} as T2
31+
expectAssignable<T1>(t2)
32+
}
33+
34+
// TODO: type-checking this is crazy slow. Figure out the cause.
35+
function testExpressionBuilderExtendsFuncArg() {
36+
type A = { a: number }
37+
type B = { b: string }
38+
type C = { c: boolean }
39+
40+
// This type extends T1 and should be assignable to it.
41+
type T2 = ExpressionBuilder<{ a: A; b: B; c: C }, 'a' | 'b' | 'c'>
42+
43+
function test(eb: ExpressionBuilder<{ a: A; b: B; c: C }, 'b' | 'c'>) {
44+
console.log(eb)
45+
}
46+
47+
const t2 = {} as T2
48+
test(t2)
49+
}
50+
51+
// TODO: type-checking this is crazy slow. Figure out the cause.
52+
async function testGenericSelectHelper() {
53+
type Parent = { id: Generated<string> }
54+
type Person = { id: Generated<string>; parent_id: string }
55+
type Pet = { owner_id: string; name: string }
56+
const db: Kysely<{ person: Person; parent: Parent; pet: Pet }> = undefined!
57+
58+
function personPetSelect(
59+
eb: ExpressionBuilder<
60+
{
61+
parent: { id: string | null }
62+
petJoin: { name: string | null }
63+
},
64+
'parent' | 'petJoin'
65+
>
66+
) {
67+
return ['parent.id'] as const
68+
}
69+
70+
const result = await db
71+
.selectFrom('parent')
72+
.leftJoin('person as personJoin', 'personJoin.parent_id', 'parent.id')
73+
.leftJoin('pet as petJoin', 'petJoin.owner_id', 'personJoin.id')
74+
.select(personPetSelect)
75+
.execute()
76+
}
77+
78+
async function testGenericSelect<T extends keyof Database>(
79+
db: Kysely<Database>,
80+
table: T
81+
) {
82+
const r1 = await db.selectFrom(table).select('id').executeTakeFirstOrThrow()
83+
expectAssignable<string | number>(r1.id)
84+
}
85+
86+
async function testGenericUpdate(db: Kysely<Database>, table: 'pet' | 'movie') {
87+
await db.updateTable(table).set({ id: '123' }).execute()
88+
}
89+
90+
async function testSelectsInVariable(db: Kysely<Database>) {
91+
const selects = [
92+
'first_name',
93+
(eb: ExpressionBuilder<Database, 'person'>) =>
94+
eb
95+
.selectFrom('pet')
96+
.select('name')
97+
.whereRef('pet.owner_id', '=', 'person.id')
98+
.as('pet_name'),
99+
] as const
100+
101+
const r1 = await db
102+
.selectFrom('person')
103+
.select(selects)
104+
.executeTakeFirstOrThrow()
105+
106+
expectType<{ first_name: string; pet_name: string | null }>(r1)
107+
}

test/typings/test-d/index.test-d.ts

-31
Original file line numberDiff line numberDiff line change
@@ -351,37 +351,6 @@ async function testCall(db: Kysely<Database>) {
351351
expectType<{ species: 'dog' | 'cat'; name: string }>(r1)
352352
}
353353

354-
async function testGenericSelect<T extends keyof Database>(
355-
db: Kysely<Database>,
356-
table: T
357-
) {
358-
const r1 = await db.selectFrom(table).select('id').executeTakeFirstOrThrow()
359-
expectAssignable<string | number>(r1.id)
360-
}
361-
362-
async function testGenericUpdate(db: Kysely<Database>, table: 'pet' | 'movie') {
363-
await db.updateTable(table).set({ id: '123' }).execute()
364-
}
365-
366-
async function testSelectsInVariable(db: Kysely<Database>) {
367-
const selects = [
368-
'first_name',
369-
(eb: ExpressionBuilder<Database, 'person'>) =>
370-
eb
371-
.selectFrom('pet')
372-
.select('name')
373-
.whereRef('pet.owner_id', '=', 'person.id')
374-
.as('pet_name'),
375-
] as const
376-
377-
const r1 = await db
378-
.selectFrom('person')
379-
.select(selects)
380-
.executeTakeFirstOrThrow()
381-
382-
expectType<{ first_name: string; pet_name: string | null }>(r1)
383-
}
384-
385354
async function testUntypedKysely(db: Kysely<any>) {
386355
// Kysely instance with `any` DB type still extracts column names.
387356
const r1 = await db

0 commit comments

Comments
 (0)