From 76b38c6d0d62b12db197a1d87d719c566c4a5815 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: Thu, 30 Mar 2023 18:56:31 +0800 Subject: [PATCH] refactor: style --- packages/dts-test/defineComponent.test-d.tsx | 15 ++++++--------- packages/dts-test/functionalComponent.test-d.tsx | 6 ++++-- packages/dts-test/setupHelpers.test-d.ts | 7 ++----- packages/runtime-core/src/apiSetupHelpers.ts | 4 ++-- packages/runtime-core/src/component.ts | 2 +- packages/runtime-core/src/componentSlots.ts | 13 +++++++------ packages/runtime-core/src/h.ts | 2 +- 7 files changed, 23 insertions(+), 26 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index bd6488b7f35..b9470cddbfa 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1411,20 +1411,17 @@ export default { describe('slots', () => { defineComponent({ slots: Object as SlotsType<{ - default: [foo: string, bar: number] - item: [number] + default: { foo: string; bar: number } + item: { data: number } }>, setup(props, { slots }) { - expectType any)>(slots.default) - expectType any)>(slots.item) + expectType any)>( + slots.default + ) + expectType any)>(slots.item) } }) - defineComponent({ - // @ts-expect-error `default` should be an array - slots: Object as SlotsType<{ default: string }> - }) - defineComponent({ setup(props, { slots }) { // unknown slots diff --git a/packages/dts-test/functionalComponent.test-d.tsx b/packages/dts-test/functionalComponent.test-d.tsx index 3087dcf43f6..37e2c9c9f91 100644 --- a/packages/dts-test/functionalComponent.test-d.tsx +++ b/packages/dts-test/functionalComponent.test-d.tsx @@ -69,11 +69,13 @@ const Qux: FunctionalComponent<{}, ['foo', 'bar']> = (props, { emit }) => { expectType(Qux) -const Quux: FunctionalComponent<{}, {}, { default: [foo: number] }> = ( +const Quux: FunctionalComponent<{}, {}, { default: { foo: number } }> = ( props, { emit, slots } ) => { - expectType<{ default: undefined | ((foo: number) => VNode[]) }>(slots) + expectType<{ default: undefined | ((scope: { foo: number }) => VNode[]) }>( + slots + ) } expectType(Quux) ; diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index 0edd5017f01..f1b79563a52 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -164,15 +164,12 @@ describe('defineEmits w/ runtime declaration', () => { describe('defineSlots', () => { const slots = defineSlots<{ - default: [foo: string, bar: number] + default: { foo: string; bar: number } }>() - expectType<(foo: string, bar: number) => VNode[]>(slots.default) + expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default) const slotsUntype = defineSlots() expectType(slotsUntype) - - // @ts-expect-error `default` should be an array - defineSlots<{ default: string }>() }) describe('useAttrs', () => { diff --git a/packages/runtime-core/src/apiSetupHelpers.ts b/packages/runtime-core/src/apiSetupHelpers.ts index c904a7cf4b6..41caf654d47 100644 --- a/packages/runtime-core/src/apiSetupHelpers.ts +++ b/packages/runtime-core/src/apiSetupHelpers.ts @@ -174,10 +174,10 @@ export function defineOptions< } export function defineSlots< - T extends Record = Record + T extends Record = Record >(): // @ts-expect-error Readonly<{ - [K in keyof T]: Slot + [K in keyof T]: Slot<[T[K]]> }> { if (__DEV__) { warnRuntimeUsage(`defineSlots`) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 068ccc64c85..1e5e973e81a 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -125,7 +125,7 @@ export interface ComponentInternalOptions { export interface FunctionalComponent< P = {}, E extends EmitsOptions = {}, - S extends Record = Record + S extends Record = Record > extends ComponentInternalOptions { // use of any here is intentional so it can be a valid JSX Element constructor (props: P, ctx: Omit>, 'expose'>): any diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index 696b9558cca..aa0c8b21e3c 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -23,7 +23,9 @@ import { isHmrUpdating } from './hmr' import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig' import { toRaw } from '@vue/reactivity' -export type Slot = (...args: T) => VNode[] +export type Slot = ( + ...args: T extends [any] ? any[] : T +) => VNode[] export type InternalSlots = { [name: string]: Slot | undefined @@ -32,17 +34,16 @@ export type InternalSlots = { export type Slots = Readonly declare const SlotSymbol: unique symbol -export type SlotsType = Record> = - { - [SlotSymbol]?: T - } +export type SlotsType = Record> = { + [SlotSymbol]?: T +} export type TypedSlots = [keyof S] extends [never] ? Slots : Readonly< Prettify<{ [K in keyof NonNullable]: - | Slot[K]> + | Slot<[NonNullable[K]]> | undefined }> > diff --git a/packages/runtime-core/src/h.ts b/packages/runtime-core/src/h.ts index c3c65cdfdd5..73b27107b8b 100644 --- a/packages/runtime-core/src/h.ts +++ b/packages/runtime-core/src/h.ts @@ -123,7 +123,7 @@ export function h( export function h< P, E extends EmitsOptions = {}, - S extends Record = {} + S extends Record = {} >( type: FunctionalComponent, props?: (RawProps & P) | ({} extends P ? null : never),