1
1
import { describe , expectTypeOf , it } from "vitest"
2
+ import { z } from "zod"
2
3
import { createCollection } from "../src/index"
3
4
import { localOnlyCollectionOptions } from "../src/local-only"
4
5
import type { LocalOnlyCollectionUtils } from "../src/local-only"
5
6
import type { Collection } from "../src/index"
7
+ import type { Query } from "../src/query/builder"
6
8
7
9
interface TestItem extends Record < string , unknown > {
8
10
id : number
@@ -33,7 +35,7 @@ describe(`LocalOnly Collection Types`, () => {
33
35
expectTypeOf ( options ) . toHaveProperty ( `getKey` )
34
36
35
37
// Test that getKey returns the correct type
36
- expectTypeOf ( options . getKey ) . toMatchTypeOf < ( item : TestItem ) => number > ( )
38
+ expectTypeOf ( options . getKey ) . toExtend < ( item : TestItem ) => number > ( )
37
39
} )
38
40
39
41
it ( `should be compatible with createCollection` , ( ) => {
@@ -56,7 +58,7 @@ describe(`LocalOnly Collection Types`, () => {
56
58
> ( options )
57
59
58
60
// Test that the collection has the expected type
59
- expectTypeOf ( collection ) . toMatchTypeOf <
61
+ expectTypeOf ( collection ) . toExtend <
60
62
Collection < TestItem , number , LocalOnlyCollectionUtils >
61
63
> ( )
62
64
} )
@@ -82,7 +84,7 @@ describe(`LocalOnly Collection Types`, () => {
82
84
LocalOnlyCollectionUtils
83
85
> ( options )
84
86
85
- expectTypeOf ( collection ) . toMatchTypeOf <
87
+ expectTypeOf ( collection ) . toExtend <
86
88
Collection < TestItem , number , LocalOnlyCollectionUtils >
87
89
> ( )
88
90
} )
@@ -106,7 +108,7 @@ describe(`LocalOnly Collection Types`, () => {
106
108
LocalOnlyCollectionUtils
107
109
> ( options )
108
110
109
- expectTypeOf ( collection ) . toMatchTypeOf <
111
+ expectTypeOf ( collection ) . toExtend <
110
112
Collection < TestItem , number , LocalOnlyCollectionUtils >
111
113
> ( )
112
114
} )
@@ -129,9 +131,130 @@ describe(`LocalOnly Collection Types`, () => {
129
131
LocalOnlyCollectionUtils
130
132
> ( options )
131
133
132
- expectTypeOf ( collection ) . toMatchTypeOf <
134
+ expectTypeOf ( collection ) . toExtend <
133
135
Collection < TestItem , string , LocalOnlyCollectionUtils >
134
136
> ( )
135
- expectTypeOf ( options . getKey ) . toMatchTypeOf < ( item : TestItem ) => string > ( )
137
+ expectTypeOf ( options . getKey ) . toExtend < ( item : TestItem ) => string > ( )
138
+ } )
139
+
140
+ it ( `should work with schema and infer correct types` , ( ) => {
141
+ const testSchema = z . object ( {
142
+ id : z . string ( ) ,
143
+ entityId : z . string ( ) ,
144
+ value : z . string ( ) ,
145
+ } )
146
+
147
+ const config = {
148
+ id : `test-with-schema` ,
149
+ getKey : ( item : any ) => item . id ,
150
+ schema : testSchema ,
151
+ }
152
+
153
+ const options = localOnlyCollectionOptions ( config )
154
+ const collection = createCollection ( options )
155
+
156
+ // Test that the collection has the correct inferred type from schema
157
+ expectTypeOf ( collection ) . toExtend <
158
+ Collection <
159
+ {
160
+ id : string
161
+ entityId : string
162
+ value : string
163
+ } ,
164
+ string ,
165
+ LocalOnlyCollectionUtils
166
+ >
167
+ > ( )
168
+ } )
169
+
170
+ it ( `should work with schema and query builder type inference (bug report reproduction)` , ( ) => {
171
+ const testSchema = z . object ( {
172
+ id : z . string ( ) ,
173
+ entityId : z . string ( ) ,
174
+ value : z . string ( ) ,
175
+ createdAt : z . date ( ) ,
176
+ } )
177
+
178
+ const config = {
179
+ id : `test-with-schema-query` ,
180
+ getKey : ( item : any ) => item . id ,
181
+ schema : testSchema ,
182
+ }
183
+
184
+ const options = localOnlyCollectionOptions ( config )
185
+ const collection = createCollection ( options )
186
+
187
+ // This should work without type errors - the query builder should infer the correct type
188
+ const query = ( q : InstanceType < typeof Query > ) =>
189
+ q
190
+ . from ( { bookmark : collection } )
191
+ . orderBy ( ( { bookmark } ) => bookmark . createdAt , `desc` )
192
+
193
+ // Test that the collection has the correct inferred type from schema
194
+ expectTypeOf ( collection ) . toExtend <
195
+ Collection <
196
+ {
197
+ id : string
198
+ entityId : string
199
+ value : string
200
+ createdAt : Date
201
+ } ,
202
+ string ,
203
+ LocalOnlyCollectionUtils
204
+ >
205
+ > ( )
206
+
207
+ // Test that the query builder can access the createdAt property
208
+ expectTypeOf ( query ) . toBeFunction ( )
209
+ } )
210
+
211
+ it ( `should reproduce exact bug report scenario` , ( ) => {
212
+ // This reproduces the exact scenario from the bug report
213
+ const selectUrlSchema = z . object ( {
214
+ id : z . string ( ) ,
215
+ url : z . string ( ) ,
216
+ title : z . string ( ) ,
217
+ createdAt : z . date ( ) ,
218
+ } )
219
+
220
+ const initialData = [
221
+ {
222
+ id : `1` ,
223
+ url : `https://example.com` ,
224
+ title : `Example` ,
225
+ createdAt : new Date ( ) ,
226
+ } ,
227
+ ]
228
+
229
+ const bookmarkCollection = createCollection (
230
+ localOnlyCollectionOptions ( {
231
+ initialData,
232
+ getKey : ( url : any ) => url . id ,
233
+ schema : selectUrlSchema ,
234
+ } )
235
+ )
236
+
237
+ // This should work without type errors - the query builder should infer the correct type
238
+ const query = ( q : InstanceType < typeof Query > ) =>
239
+ q
240
+ . from ( { bookmark : bookmarkCollection } )
241
+ . orderBy ( ( { bookmark } ) => bookmark . createdAt , `desc` )
242
+
243
+ // Test that the collection has the correct inferred type from schema
244
+ expectTypeOf ( bookmarkCollection ) . toExtend <
245
+ Collection <
246
+ {
247
+ id : string
248
+ url : string
249
+ title : string
250
+ createdAt : Date
251
+ } ,
252
+ string ,
253
+ LocalOnlyCollectionUtils
254
+ >
255
+ > ( )
256
+
257
+ // Test that the query builder can access the createdAt property
258
+ expectTypeOf ( query ) . toBeFunction ( )
136
259
} )
137
260
} )
0 commit comments