From 527170c102fa64df0bf7a2e90c52d7d40cf1b576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Fri, 31 Mar 2023 02:45:03 +0800 Subject: [PATCH] feat: undefined slot scope --- packages/dts-test/defineComponent.test-d.tsx | 29 ++++++++++++++++++++ packages/runtime-core/src/componentSlots.ts | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index add9c8a3fc0..32912dd9a2a 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -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[]>( @@ -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(new comp1().$slots) } }) diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index 1a0db39cadb..e48fae98ee7 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -25,7 +25,7 @@ import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig' import { toRaw } from '@vue/reactivity' export type Slot = ( - ...args: IfAny + ...args: IfAny ) => VNode[] export type InternalSlots = { @@ -44,7 +44,7 @@ export type TypedSlots = [keyof S] extends [never] : Readonly< Prettify<{ [K in keyof NonNullable]: Slot< - NonNullable[K]> + NonNullable[K] > }> >