Skip to content

Commit

Permalink
feat: undefined slot scope
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 30, 2023
1 parent 21ef29b commit 527170c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions packages/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,8 @@ describe('slots', () => {
slots: Object as SlotsType<{
default: { foo: string; bar: number }
optional?: { data: string }
undefinedScope: undefined | { data: string }
optionalUndefinedScope?: undefined | { data: string }
}>,
setup(props, { slots }) {
expectType<(scope: { foo: string; bar: number }) => VNode[]>(
Expand All @@ -1429,6 +1431,33 @@ describe('slots', () => {
slots.optional({ data: 'foo' })
slots.optional?.({ data: 'foo' })

expectType<{
(): VNode[]
(scope: undefined | { data: string }): VNode[]
}>(slots.undefinedScope)

expectType<
| { (): VNode[]; (scope: undefined | { data: string }): VNode[] }
| undefined
>(slots.optionalUndefinedScope)

slots.default({ foo: 'foo', bar: 1 })
// @ts-expect-error it's optional
slots.optional({ data: 'foo' })
slots.optional?.({ data: 'foo' })
slots.undefinedScope()
slots.undefinedScope(undefined)
// @ts-expect-error
slots.undefinedScope('foo')

slots.optionalUndefinedScope?.()
slots.optionalUndefinedScope?.(undefined)
slots.optionalUndefinedScope?.({ data: 'foo' })
// @ts-expect-error
slots.optionalUndefinedScope()
// @ts-expect-error
slots.optionalUndefinedScope?.('foo')

expectType<typeof slots | undefined>(new comp1().$slots)
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig'
import { toRaw } from '@vue/reactivity'

export type Slot<T extends any = any> = (
...args: IfAny<T, any[], [T]>
...args: IfAny<T, any[], [T] | (T extends undefined ? [] : never)>
) => VNode[]

export type InternalSlots = {
Expand All @@ -44,7 +44,7 @@ export type TypedSlots<S extends SlotsType> = [keyof S] extends [never]
: Readonly<
Prettify<{
[K in keyof NonNullable<S[typeof SlotSymbol]>]: Slot<
NonNullable<NonNullable<S[typeof SlotSymbol]>[K]>
NonNullable<S[typeof SlotSymbol]>[K]
>
}>
>
Expand Down

0 comments on commit 527170c

Please sign in to comment.