From df90c7c35539b321cbadcc515a3805aef81d5dae Mon Sep 17 00:00:00 2001 From: rudyxu Date: Fri, 1 Sep 2023 13:15:58 +0800 Subject: [PATCH] chore: improve code --- packages/runtime-core/src/apiDefineComponent.ts | 17 ++++++++--------- packages/runtime-core/src/component.ts | 9 ++++++--- packages/runtime-core/src/componentOptions.ts | 16 ++++++++-------- .../runtime-core/src/componentPublicInstance.ts | 14 ++++++++------ packages/runtime-dom/src/apiCustomElement.ts | 13 +++++-------- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index bf1787d8027..ef31745c456 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -15,8 +15,7 @@ import { import { SetupContext, AllowedComponentProps, - ComponentCustomProps, - Data + ComponentCustomProps } from './component' import { ExtractPropTypes, @@ -59,7 +58,7 @@ export type DefineComponent< Props = ResolveProps, Defaults = ExtractDefaultPropTypes, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined > = ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, @@ -109,10 +108,10 @@ export function defineComponent< E extends EmitsOptions = {}, EE extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, - PropsAttrs = IsSameType> extends true + Attrs extends AttrsType | undefined = undefined, + PropsAttrs = IsSameType extends true ? {} - : UnwrapAttrsType + : UnwrapAttrsType> >( setup: ( props: Props, @@ -156,7 +155,7 @@ export function defineComponent< E extends EmitsOptions = {}, EE extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, I extends ComponentInjectOptions = {}, II extends string = string >( @@ -206,7 +205,7 @@ export function defineComponent< E extends EmitsOptions = {}, EE extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, I extends ComponentInjectOptions = {}, II extends string = string, Props = Readonly<{ [key in PropNames]?: any }> @@ -260,7 +259,7 @@ export function defineComponent< S extends SlotsType = {}, I extends ComponentInjectOptions = {}, II extends string = string, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined >( options: ComponentOptionsWithObjectProps< PropsOptions, diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 6b4b3316235..bb79234e792 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -19,7 +19,8 @@ import { exposeSetupStateOnRenderContext, ComponentPublicInstanceConstructor, publicPropertiesMap, - RuntimeCompiledPublicInstanceProxyHandlers + RuntimeCompiledPublicInstanceProxyHandlers, + IsSameType } from './componentPublicInstance' import { ComponentPropsOptions, @@ -187,10 +188,12 @@ type LifecycleHook = TFn[] | null export type SetupContext< E = EmitsOptions, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined > = E extends any ? { - attrs: Partial> + attrs: IsSameType extends true + ? Data + : UnwrapAttrsType> slots: UnwrapSlotsType emit: EmitFn expose: (exposed?: Record) => void diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index d64d8e12b04..4208e45ca2a 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -112,7 +112,7 @@ export interface ComponentOptionsBase< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined > extends LegacyOptions, ComponentInternalOptions, ComponentCustomOptions { @@ -226,7 +226,7 @@ export type ComponentOptionsWithoutProps< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, PE = Props & EmitsToProps > = ComponentOptionsBase< PE, @@ -277,7 +277,7 @@ export type ComponentOptionsWithArrayProps< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, Props = Prettify>> > = ComponentOptionsBase< Props, @@ -328,7 +328,7 @@ export type ComponentOptionsWithObjectProps< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, Props = Prettify & EmitsToProps>>, Defaults = ExtractDefaultPropTypes > = ComponentOptionsBase< @@ -421,17 +421,17 @@ declare const AttrSymbol: unique symbol export type AttrsType = Record> = { [AttrSymbol]?: T } + export type UnwrapAttrsType< - S extends AttrsType, - T = NonNullable -> = [keyof S] extends [never] + Attrs extends AttrsType, + T = NonNullable +> = [keyof Attrs] extends [never] ? Data : Readonly< Prettify<{ [K in keyof T]: T[K] }> > - export type ComputedOptions = Record< string, ComputedGetter | WritableComputedOptions diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index d5264e91550..913bd7fbec4 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -152,7 +152,7 @@ export type CreateComponentPublicInstance< MakeDefaultsOptional extends boolean = false, I extends ComponentInjectOptions = {}, S extends SlotsType = {}, - Attrs extends AttrsType = {}, + Attrs extends AttrsType | undefined = undefined, PublicMixin = IntersectionMixin & IntersectionMixin, PublicP = UnwrapMixinsType & EnsureNonVoid

, PublicB = UnwrapMixinsType & EnsureNonVoid, @@ -215,10 +215,10 @@ export type ComponentPublicInstance< Options = ComponentOptionsBase, I extends ComponentInjectOptions = {}, S extends SlotsType = {}, - Attrs extends AttrsType = {}, - PropsAttrs = IsSameType, Data> extends true + Attrs extends AttrsType | undefined = undefined, + PropsAttrs = IsSameType extends true ? {} - : Omit, keyof (P & PublicProps)> + : Omit>, keyof (P & PublicProps)> > = { $: ComponentInternalInstance $data: D @@ -227,8 +227,10 @@ export type ComponentPublicInstance< ? Partial & Omit

& PropsAttrs : P & PublicProps & PropsAttrs > - $attrs: Omit, keyof (P & PublicProps)> & - AllowedComponentProps + $attrs: IsSameType extends true + ? Data + : Omit>, keyof (P & PublicProps)> & + AllowedComponentProps $refs: Data $slots: UnwrapSlotsType $root: ComponentPublicInstance | null diff --git a/packages/runtime-dom/src/apiCustomElement.ts b/packages/runtime-dom/src/apiCustomElement.ts index 2e23e213102..7aaaaacb87f 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -35,14 +35,11 @@ export type VueElementConstructor

= { // so most of the following overloads should be kept in sync w/ defineComponent. // overload 1: direct setup function -export function defineCustomElement< - Props, - RawBindings = object, ->( +export function defineCustomElement( setup: ( props: Readonly, ctx: SetupContext - ) => RawBindings | RenderFunction, + ) => RawBindings | RenderFunction ): VueElementConstructor // overload 2: object format with no props @@ -59,7 +56,7 @@ export function defineCustomElement< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined >( options: ComponentOptionsWithoutProps< Props, @@ -92,7 +89,7 @@ export function defineCustomElement< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined >( options: ComponentOptionsWithArrayProps< PropNames, @@ -125,7 +122,7 @@ export function defineCustomElement< I extends ComponentInjectOptions = {}, II extends string = string, S extends SlotsType = {}, - Attrs extends AttrsType = {} + Attrs extends AttrsType | undefined = undefined >( options: ComponentOptionsWithObjectProps< PropsOptions,