From 1279b1730079f77692a0817d51bbba57eb2b871b Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 8 May 2023 11:53:49 +0800 Subject: [PATCH] fix(types): remove short syntax support in defineSlots() ref: https://github.com/vuejs/language-tools/issues/2758 --- packages/dts-test/setupHelpers.test-d.ts | 8 -------- packages/runtime-core/src/apiSetupHelpers.ts | 4 ++-- packages/runtime-core/src/component.ts | 4 ++-- packages/runtime-core/src/componentPublicInstance.ts | 4 ++-- packages/runtime-core/src/componentSlots.ts | 7 ++++++- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index f738e58b9eb..9b68b345268 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -186,14 +186,6 @@ describe('defineEmits w/ runtime declaration', () => { }) describe('defineSlots', () => { - // short syntax - const slots = defineSlots<{ - default: { foo: string; bar: number } - optional?: string - }>() - expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default) - expectType VNode[])>(slots.optional) - // literal fn syntax (allow for specifying return type) const fnSlots = defineSlots<{ default(props: { foo: string; bar: number }): any diff --git a/packages/runtime-core/src/apiSetupHelpers.ts b/packages/runtime-core/src/apiSetupHelpers.ts index de7426ad325..554115240b3 100644 --- a/packages/runtime-core/src/apiSetupHelpers.ts +++ b/packages/runtime-core/src/apiSetupHelpers.ts @@ -28,7 +28,7 @@ import { PropOptions } from './componentProps' import { warn } from './warning' -import { SlotsType, TypedSlots } from './componentSlots' +import { SlotsType, StrictUnwrapSlotsType } from './componentSlots' import { Ref, ref } from '@vue/reactivity' import { watch } from './apiWatch' @@ -205,7 +205,7 @@ export function defineOptions< export function defineSlots< S extends Record = Record ->(): TypedSlots> { +>(): StrictUnwrapSlotsType> { if (__DEV__) { warnRuntimeUsage(`defineSlots`) } diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 684f028fd5b..33229630e49 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -32,7 +32,7 @@ import { InternalSlots, Slots, SlotsType, - TypedSlots + UnwrapSlotsType } from './componentSlots' import { warn } from './warning' import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling' @@ -188,7 +188,7 @@ export type SetupContext< > = E extends any ? { attrs: Data - slots: TypedSlots + slots: UnwrapSlotsType emit: EmitFn expose: (exposed?: Record) => void } diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index dd2d29670e6..79bcedda759 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -40,7 +40,7 @@ import { ComponentInjectOptions } from './componentOptions' import { EmitsOptions, EmitFn } from './componentEmits' -import { SlotsType, TypedSlots } from './componentSlots' +import { SlotsType, UnwrapSlotsType } from './componentSlots' import { markAttrsAccessed } from './componentRenderUtils' import { currentRenderingInstance } from './componentRenderContext' import { warn } from './warning' @@ -213,7 +213,7 @@ export type ComponentPublicInstance< > $attrs: Data $refs: Data - $slots: TypedSlots + $slots: UnwrapSlotsType $root: ComponentPublicInstance | null $parent: ComponentPublicInstance | null $emit: EmitFn diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index 8f59099d833..afc5f03933b 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -41,7 +41,12 @@ export type SlotsType = Record> = { [SlotSymbol]?: T } -export type TypedSlots< +export type StrictUnwrapSlotsType< + S extends SlotsType, + T = NonNullable +> = [keyof S] extends [never] ? Slots : Readonly + +export type UnwrapSlotsType< S extends SlotsType, T = NonNullable > = [keyof S] extends [never]