Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime core) : improve slot type definitions #11725

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2027,3 +2027,35 @@ expectString(instance.actionText)
// public prop on $props should be optional
// @ts-expect-error
expectString(instance.$props.actionText)

describe('with Slot', () => {
defineComponent({
slots: Object as SlotsType<{
default: { foo: string; bar: number }
optional?: { data: string }
}>,
setup(props, { slots }) {
expectType<
(scope: {
foo: string
bar: number
}) => VNode[] | VNode | null | undefined
>(slots.default)
expectType<
| ((scope: { data: string }) => VNode[] | VNode | null | undefined)
| undefined
>(slots.optional)

// Test slot invocation
const defaultResult = slots.default({ foo: 'foo', bar: 1 })
expectType<VNode[] | VNode | null | undefined>(defaultResult)

const optionalResult = slots.optional?.({ data: 'foo' })
expectType<VNode[] | VNode | null | undefined | undefined>(optionalResult)

// Test that single VNode, null, and undefined are allowed
slots.default({ foo: 'foo', bar: 1 })
slots.optional?.({ data: 'foo' })
},
})
})
20 changes: 14 additions & 6 deletions packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type Slot<T extends any = any> = (
...args: IfAny<T, any[], [T] | (T extends undefined ? [] : never)>
) => VNode[]

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

export type InternalSlots = {
[name: string]: Slot | undefined
}
Expand Down Expand Up @@ -84,14 +88,14 @@ const normalizeSlotValue = (value: unknown): VNode[] =>
? value.map(normalizeVNode)
: [normalizeVNode(value as VNodeChild)]

const normalizeSlot = (
const normalizeSlot = <T extends any>(
key: string,
rawSlot: Function,
rawSlot: FlexibleSlot<T>,
ctx: ComponentInternalInstance | null | undefined,
): Slot => {
if ((rawSlot as any)._n) {
// already normalized - #5353
return rawSlot as Slot
return rawSlot as Slot<T>
}
const normalized = withCtx((...args: any[]) => {
if (
Expand All @@ -105,8 +109,12 @@ const normalizeSlot = (
`Invoke the slot function inside the render function instead.`,
)
}
return normalizeSlotValue(rawSlot(...args))
}, ctx) as Slot
return normalizeSlotValue(
rawSlot(
...(args as IfAny<T, any[], [T] | (T extends undefined ? [] : never)>),
),
)
}, ctx) as Slot<T>
// NOT a compiled slot
;(normalized as ContextualRenderFn)._c = false
return normalized
Expand All @@ -122,7 +130,7 @@ const normalizeObjectSlots = (
if (isInternalKey(key)) continue
const value = rawSlots[key]
if (isFunction(value)) {
slots[key] = normalizeSlot(key, value, ctx)
slots[key] = normalizeSlot(key, value as Slot, ctx)
} else if (value != null) {
if (
__DEV__ &&
Expand Down