Skip to content

Commit 527170c

Browse files
committed
feat: undefined slot scope
1 parent 21ef29b commit 527170c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/dts-test/defineComponent.test-d.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,8 @@ describe('slots', () => {
14141414
slots: Object as SlotsType<{
14151415
default: { foo: string; bar: number }
14161416
optional?: { data: string }
1417+
undefinedScope: undefined | { data: string }
1418+
optionalUndefinedScope?: undefined | { data: string }
14171419
}>,
14181420
setup(props, { slots }) {
14191421
expectType<(scope: { foo: string; bar: number }) => VNode[]>(
@@ -1429,6 +1431,33 @@ describe('slots', () => {
14291431
slots.optional({ data: 'foo' })
14301432
slots.optional?.({ data: 'foo' })
14311433

1434+
expectType<{
1435+
(): VNode[]
1436+
(scope: undefined | { data: string }): VNode[]
1437+
}>(slots.undefinedScope)
1438+
1439+
expectType<
1440+
| { (): VNode[]; (scope: undefined | { data: string }): VNode[] }
1441+
| undefined
1442+
>(slots.optionalUndefinedScope)
1443+
1444+
slots.default({ foo: 'foo', bar: 1 })
1445+
// @ts-expect-error it's optional
1446+
slots.optional({ data: 'foo' })
1447+
slots.optional?.({ data: 'foo' })
1448+
slots.undefinedScope()
1449+
slots.undefinedScope(undefined)
1450+
// @ts-expect-error
1451+
slots.undefinedScope('foo')
1452+
1453+
slots.optionalUndefinedScope?.()
1454+
slots.optionalUndefinedScope?.(undefined)
1455+
slots.optionalUndefinedScope?.({ data: 'foo' })
1456+
// @ts-expect-error
1457+
slots.optionalUndefinedScope()
1458+
// @ts-expect-error
1459+
slots.optionalUndefinedScope?.('foo')
1460+
14321461
expectType<typeof slots | undefined>(new comp1().$slots)
14331462
}
14341463
})

packages/runtime-core/src/componentSlots.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig'
2525
import { toRaw } from '@vue/reactivity'
2626

2727
export type Slot<T extends any = any> = (
28-
...args: IfAny<T, any[], [T]>
28+
...args: IfAny<T, any[], [T] | (T extends undefined ? [] : never)>
2929
) => VNode[]
3030

3131
export type InternalSlots = {
@@ -44,7 +44,7 @@ export type TypedSlots<S extends SlotsType> = [keyof S] extends [never]
4444
: Readonly<
4545
Prettify<{
4646
[K in keyof NonNullable<S[typeof SlotSymbol]>]: Slot<
47-
NonNullable<NonNullable<S[typeof SlotSymbol]>[K]>
47+
NonNullable<S[typeof SlotSymbol]>[K]
4848
>
4949
}>
5050
>

0 commit comments

Comments
 (0)