@@ -127,32 +127,85 @@ export interface Collection<
127127 *
128128 */
129129
130- // Overload for when schema is provided
130+ // Overload for when schema is provided and utils is required (not optional)
131+ // We can't infer the Utils type from the CollectionConfig because it will always be optional
132+ // So we omit it from that type and instead infer it from the extension `& { utils: TUtils }`
133+ // such that we have the real, non-optional Utils type
131134export function createCollection <
132135 T extends StandardSchemaV1 ,
133- TKey extends string | number = string | number ,
134- TUtils extends UtilsRecord = UtilsRecord ,
136+ TKey extends string | number ,
137+ TUtils extends UtilsRecord ,
135138> (
136- options : CollectionConfig < InferSchemaOutput < T > , TKey , T , TUtils > & {
139+ options : Omit <
140+ CollectionConfig < InferSchemaOutput < T > , TKey , T , TUtils > ,
141+ `utils`
142+ > & {
137143 schema : T
138- utils ? : TUtils
144+ utils : TUtils // Required utils
139145 } & NonSingleResult
140146) : Collection < InferSchemaOutput < T > , TKey , TUtils , T , InferSchemaInput < T > > &
141147 NonSingleResult
142148
149+ // Overload for when schema is provided and utils is optional
150+ // In this case we can simply infer the Utils type from the CollectionConfig type
151+ export function createCollection <
152+ T extends StandardSchemaV1 ,
153+ TKey extends string | number ,
154+ TUtils extends UtilsRecord ,
155+ > (
156+ options : CollectionConfig < InferSchemaOutput < T > , TKey , T , TUtils > & {
157+ schema : T
158+ } & NonSingleResult
159+ ) : Collection <
160+ InferSchemaOutput < T > ,
161+ TKey ,
162+ Exclude < TUtils , undefined > ,
163+ T ,
164+ InferSchemaInput < T >
165+ > &
166+ NonSingleResult
167+
168+ // Overload for when schema is provided, singleResult is true, and utils is required
169+ export function createCollection <
170+ T extends StandardSchemaV1 ,
171+ TKey extends string | number ,
172+ TUtils extends UtilsRecord ,
173+ > (
174+ options : Omit <
175+ CollectionConfig < InferSchemaOutput < T > , TKey , T , TUtils > ,
176+ `utils`
177+ > & {
178+ schema : T
179+ utils : TUtils // Required utils
180+ } & SingleResult
181+ ) : Collection < InferSchemaOutput < T > , TKey , TUtils , T , InferSchemaInput < T > > &
182+ SingleResult
183+
143184// Overload for when schema is provided and singleResult is true
144185export function createCollection <
145186 T extends StandardSchemaV1 ,
146- TKey extends string | number = string | number ,
147- TUtils extends UtilsRecord = UtilsRecord ,
187+ TKey extends string | number ,
188+ TUtils extends UtilsRecord ,
148189> (
149190 options : CollectionConfig < InferSchemaOutput < T > , TKey , T , TUtils > & {
150191 schema : T
151- utils ?: TUtils
152192 } & SingleResult
153193) : Collection < InferSchemaOutput < T > , TKey , TUtils , T , InferSchemaInput < T > > &
154194 SingleResult
155195
196+ // Overload for when no schema is provided and utils is required
197+ // the type T needs to be passed explicitly unless it can be inferred from the getKey function in the config
198+ export function createCollection <
199+ T extends object ,
200+ TKey extends string | number ,
201+ TUtils extends UtilsRecord ,
202+ > (
203+ options : Omit < CollectionConfig < T , TKey , never , TUtils > , `utils`> & {
204+ schema ?: never // prohibit schema if an explicit type is provided
205+ utils : TUtils // Required utils
206+ } & NonSingleResult
207+ ) : Collection < T , TKey , TUtils , never , T > & NonSingleResult
208+
156209// Overload for when no schema is provided
157210// the type T needs to be passed explicitly unless it can be inferred from the getKey function in the config
158211export function createCollection <
@@ -162,10 +215,22 @@ export function createCollection<
162215> (
163216 options : CollectionConfig < T , TKey , never , TUtils > & {
164217 schema ?: never // prohibit schema if an explicit type is provided
165- utils ?: TUtils
166218 } & NonSingleResult
167219) : Collection < T , TKey , TUtils , never , T > & NonSingleResult
168220
221+ // Overload for when no schema is provided, singleResult is true, and utils is required
222+ // the type T needs to be passed explicitly unless it can be inferred from the getKey function in the config
223+ export function createCollection <
224+ T extends object ,
225+ TKey extends string | number = string | number ,
226+ TUtils extends UtilsRecord = UtilsRecord ,
227+ > (
228+ options : Omit < CollectionConfig < T , TKey , never , TUtils > , `utils`> & {
229+ schema ?: never // prohibit schema if an explicit type is provided
230+ utils : TUtils // Required utils
231+ } & SingleResult
232+ ) : Collection < T , TKey , TUtils , never , T > & SingleResult
233+
169234// Overload for when no schema is provided and singleResult is true
170235// the type T needs to be passed explicitly unless it can be inferred from the getKey function in the config
171236export function createCollection <
@@ -175,15 +240,13 @@ export function createCollection<
175240> (
176241 options : CollectionConfig < T , TKey , never , TUtils > & {
177242 schema ?: never // prohibit schema if an explicit type is provided
178- utils ?: TUtils
179243 } & SingleResult
180244) : Collection < T , TKey , TUtils , never , T > & SingleResult
181245
182246// Implementation
183247export function createCollection (
184- options : CollectionConfig < any , string | number , any > & {
248+ options : CollectionConfig < any , string | number , any , UtilsRecord > & {
185249 schema ?: StandardSchemaV1
186- utils ?: UtilsRecord
187250 }
188251) : Collection < any , string | number , UtilsRecord , any , any > {
189252 const collection = new CollectionImpl < any , string | number , any , any , any > (
0 commit comments