Skip to content

Commit e82e8fb

Browse files
authored
fix: make mount options slots compatible with noUncheckedIndexedAccess true (#2713)
It's not necessary to define all slots, pratially defined slots are used all over the place throughout the tests here and routinely by users. However, the type for slots in mount options was not partiall and that caused type errors for people using `noUncheckedIndexedAccess: true` even though it work fine in runtime. Adding `?` doesn't change anything for users with `noUncheckedIndexedAccess: false`, it has passed type checking before and will continue to pass. Type checking will now also pass for users with partially defined slots using `noUncheckedIndexedAccess: true`.
1 parent f3013b5 commit e82e8fb

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/mount.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type ComponentMountingOptions<
2424
P extends ComponentProps<T> = ComponentProps<T>
2525
> = Omit<MountingOptions<P, ComponentData<T>>, 'slots'> & {
2626
slots?: {
27-
[K in keyof ComponentSlots<T>]: WithArray<
27+
[K in keyof ComponentSlots<T>]?: WithArray<
2828
| ShimSlotReturnType<ComponentSlots<T>[K]>
2929
| string
3030
| VNode

test-dts/mount.d-test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
ref,
1010
SetupContext,
1111
Prop,
12-
VNodeChild
12+
VNodeChild,
13+
SlotsType,
14+
VNode
1315
} from 'vue'
1416
import { Options, Vue } from 'vue-class-component'
1517
import { mount } from '../src'
@@ -340,3 +342,82 @@ expectError(
340342
}
341343
)
342344
)
345+
346+
// slots
347+
const SetupComponentWithSlots = defineComponent<
348+
{
349+
hello: string
350+
},
351+
{
352+
hallo: () => void
353+
},
354+
string,
355+
SlotsType<{
356+
foo: () => VNode[]
357+
bar: () => VNode[]
358+
baz: () => VNode[]
359+
}>
360+
>((): any => {})
361+
362+
// optional slots
363+
mount(SetupComponentWithSlots, {})
364+
365+
mount(SetupComponentWithSlots, {
366+
slots: {}
367+
})
368+
369+
mount(SetupComponentWithSlots, {
370+
slots: {
371+
foo: 'foo'
372+
}
373+
})
374+
375+
mount(SetupComponentWithSlots, {
376+
slots: {
377+
foo: 'foo',
378+
bar: 'bar'
379+
}
380+
})
381+
382+
mount(SetupComponentWithSlots, {
383+
slots: {
384+
foo: 'foo',
385+
bar: 'bar',
386+
baz: 'baz'
387+
}
388+
})
389+
390+
// extra slots
391+
mount(SetupComponentWithSlots, {
392+
slots: {
393+
// @ts-expect-error - This slot doesn't exist in the component and it should be reported.
394+
extraSlot: 'nonExistentSlot'
395+
}
396+
})
397+
398+
mount(SetupComponentWithSlots, {
399+
slots: {
400+
foo: 'foo',
401+
// @ts-expect-error - This slot doesn't exist in the component and it should be reported.
402+
extraSlot: 'nonExistentSlot'
403+
}
404+
})
405+
406+
mount(SetupComponentWithSlots, {
407+
slots: {
408+
foo: 'foo',
409+
bar: 'bar',
410+
// @ts-expect-error - This slot doesn't exist in the component and it should be reported.
411+
extraSlot: 'nonExistentSlot'
412+
}
413+
})
414+
415+
mount(SetupComponentWithSlots, {
416+
slots: {
417+
foo: 'foo',
418+
bar: 'bar',
419+
baz: 'baz',
420+
// @ts-expect-error - This slot doesn't exist in the component and it should be reported.
421+
extraSlot: 'nonExistentSlot'
422+
}
423+
})

test-dts/tsconfig.tsd.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"noEmit": false,
55
"skipLibCheck": true,
66
"experimentalDecorators": true,
7+
"noUncheckedIndexedAccess": true,
78
"strictNullChecks": false
89
},
910
"exclude": [

0 commit comments

Comments
 (0)