@@ -260,6 +260,30 @@ describe('defineSlots', () => {
260
260
expectType < Slots > ( slotsUntype )
261
261
} )
262
262
263
+ describe ( 'defineSlots generic' , < T extends Record < string , any > > ( ) => {
264
+ const props = defineProps < {
265
+ item : T
266
+ } > ( )
267
+
268
+ const slots = defineSlots <
269
+ {
270
+ [ K in keyof T as `slot-${K & string } `] ?: ( props : { item : T } ) => any
271
+ } & {
272
+ label ?: ( props : { item : T } ) => any
273
+ }
274
+ > ( )
275
+
276
+ for ( const key of Object . keys ( props . item ) as ( keyof T & string ) [ ] ) {
277
+ slots [ `slot-${ String ( key ) } ` ] ?.( {
278
+ item : props . item
279
+ } )
280
+ }
281
+ slots . label ?.( { item : props . item } )
282
+
283
+ // @ts -expect-error calling wrong slot
284
+ slots . foo ( { } )
285
+ } )
286
+
263
287
describe ( 'defineModel' , ( ) => {
264
288
// overload 1
265
289
const modelValueRequired = defineModel < boolean > ( { required : true } )
@@ -336,6 +360,78 @@ describe('useSlots', () => {
336
360
expectType < Slots > ( slots )
337
361
} )
338
362
363
+ describe ( 'defineSlots generic' , < T extends Record < string , any > > ( ) => {
364
+ const props = defineProps < {
365
+ item : T
366
+ } > ( )
367
+
368
+ const slots = defineSlots <
369
+ {
370
+ [ K in keyof T as `slot-${K & string } `] ?: ( props : { item : T } ) => any
371
+ } & {
372
+ label ?: ( props : { item : T } ) => any
373
+ }
374
+ > ( )
375
+
376
+ // @ts -expect-error slots should be readonly
377
+ slots . label = ( ) => { }
378
+
379
+ // @ts -expect-error non existing slot
380
+ slots [ 'foo-asdas' ] ?.( {
381
+ item : props . item
382
+ } )
383
+ for ( const key in props . item ) {
384
+ slots [ `slot-${ String ( key ) } ` ] ?.( {
385
+ item : props . item
386
+ } )
387
+ slots [ `slot-${ String ( key as keyof T ) } ` ] ?.( {
388
+ item : props . item
389
+ } )
390
+ }
391
+
392
+ for ( const key of Object . keys ( props . item ) as ( keyof T ) [ ] ) {
393
+ slots [ `slot-${ String ( key ) } ` ] ?.( {
394
+ item : props . item
395
+ } )
396
+ }
397
+ slots . label ?.( { item : props . item } )
398
+
399
+ // @ts -expect-error calling wrong slot
400
+ slots . foo ( { } )
401
+ } )
402
+
403
+ describe ( 'defineSlots generic strict' , < T extends {
404
+ foo : 'foo'
405
+ bar : 'bar'
406
+ } > ( ) => {
407
+ const props = defineProps < {
408
+ item : T
409
+ } > ( )
410
+
411
+ const slots = defineSlots <
412
+ {
413
+ [ K in keyof T as `slot-${K & string } `] ?: ( props : { item : T } ) => any
414
+ } & {
415
+ label ?: ( props : { item : T } ) => any
416
+ }
417
+ > ( )
418
+
419
+ // slot-bar/foo should be automatically inferred
420
+ slots [ 'slot-bar' ] ?.( { item : props . item } )
421
+ slots [ 'slot-foo' ] ?.( { item : props . item } )
422
+
423
+ slots . label ?.( { item : props . item } )
424
+
425
+ // @ts -expect-error not part of the extends
426
+ slots [ 'slot-RANDOM' ] ?.( { item : props . item } )
427
+
428
+ // @ts -expect-error slots should be readonly
429
+ slots . label = ( ) => { }
430
+
431
+ // @ts -expect-error calling wrong slot
432
+ slots . foo ( { } )
433
+ } )
434
+
339
435
// #6420
340
436
describe ( 'toRefs w/ type declaration' , ( ) => {
341
437
const props = defineProps < {
0 commit comments