From bdf557f6f233c039fff8007b1b16aec00c4e68aa Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 5 Apr 2023 22:30:50 +0800 Subject: [PATCH] fix(types): retain type parameters order for public types --- packages/dts-test/defineComponent.test-d.tsx | 2 +- .../runtime-core/src/apiDefineComponent.ts | 70 ++++++++++++++----- packages/runtime-core/src/componentOptions.ts | 6 +- .../src/componentPublicInstance.ts | 8 +-- packages/runtime-dom/src/apiCustomElement.ts | 24 +++---- 5 files changed, 72 insertions(+), 38 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 32912dd9a2a..edd8d17eb06 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1494,9 +1494,9 @@ declare const MyButton: DefineComponent< ComponentOptionsMixin, EmitsOptions, string, - {}, VNodeProps & AllowedComponentProps & ComponentCustomProps, Readonly>, + {}, {} > ; diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 0adc9d29387..5e8e7a85735 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -34,6 +34,13 @@ export type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps +type ResolveProps = Readonly< + PropsOrPropOptions extends ComponentPropsOptions + ? ExtractPropTypes + : PropsOrPropOptions +> & + ({} extends E ? {} : EmitsToProps) + export type DefineComponent< PropsOrPropOptions = {}, RawBindings = {}, @@ -44,15 +51,10 @@ export type DefineComponent< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = {}, EE extends string = string, - S extends SlotsType = {}, PP = PublicProps, - Props = Readonly< - PropsOrPropOptions extends ComponentPropsOptions - ? ExtractPropTypes - : PropsOrPropOptions - > & - ({} extends E ? {} : EmitsToProps), - Defaults = ExtractDefaultPropTypes + Props = ResolveProps, + Defaults = ExtractDefaultPropTypes, + S extends SlotsType = {} > = ComponentPublicInstanceConstructor< CreateComponentPublicInstance< Props, @@ -152,11 +154,25 @@ export function defineComponent< Extends, E, EE, - S, I, - II + II, + S > -): DefineComponent +): DefineComponent< + Props, + RawBindings, + D, + C, + M, + Mixin, + Extends, + E, + EE, + PublicProps, + ResolveProps, + ExtractDefaultPropTypes, + S +> // overload 3: object format with array props declaration // props inferred as { [key in PropNames]?: any } @@ -173,7 +189,8 @@ export function defineComponent< EE extends string = string, S extends SlotsType = {}, I extends ComponentInjectOptions = {}, - II extends string = string + II extends string = string, + Props = Readonly<{ [key in PropNames]?: any }> >( options: ComponentOptionsWithArrayProps< PropNames, @@ -185,12 +202,12 @@ export function defineComponent< Extends, E, EE, - S, I, - II + II, + S > ): DefineComponent< - Readonly<{ [key in PropNames]?: any }>, + Props, RawBindings, D, C, @@ -199,6 +216,9 @@ export function defineComponent< Extends, E, EE, + PublicProps, + ResolveProps, + ExtractDefaultPropTypes, S > @@ -230,11 +250,25 @@ export function defineComponent< Extends, E, EE, - S, I, - II + II, + S > -): DefineComponent +): DefineComponent< + PropsOptions, + RawBindings, + D, + C, + M, + Mixin, + Extends, + E, + EE, + PublicProps, + ResolveProps, + ExtractDefaultPropTypes, + S +> // implementation, close to no-op export function defineComponent( diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index 04c3839dd1d..cf459962c44 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -219,9 +219,9 @@ export type ComponentOptionsWithoutProps< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = EmitsOptions, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, II extends string = string, + S extends SlotsType = {}, PE = Props & EmitsToProps > = ComponentOptionsBase< PE, @@ -267,9 +267,9 @@ export type ComponentOptionsWithArrayProps< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = EmitsOptions, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, II extends string = string, + S extends SlotsType = {}, Props = Prettify>> > = ComponentOptionsBase< Props, @@ -315,9 +315,9 @@ export type ComponentOptionsWithObjectProps< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = EmitsOptions, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, II extends string = string, + S extends SlotsType = {}, Props = Prettify & EmitsToProps>>, Defaults = ExtractDefaultPropTypes > = ComponentOptionsBase< diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 9997296b641..7ffa3ab2cf9 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -164,12 +164,12 @@ export type CreateComponentPublicInstance< PublicC, PublicM, E, - S, PublicProps, PublicDefaults, MakeDefaultsOptional, ComponentOptionsBase, - I + I, + S > // public properties exposed on the proxy, which is used as the render context @@ -181,12 +181,12 @@ export type ComponentPublicInstance< C extends ComputedOptions = {}, M extends MethodOptions = {}, E extends EmitsOptions = {}, - S extends SlotsType = {}, PublicProps = P, Defaults = {}, MakeDefaultsOptional extends boolean = false, Options = ComponentOptionsBase, - I extends ComponentInjectOptions = {} + I extends ComponentInjectOptions = {}, + S extends SlotsType = {} > = { $: ComponentInternalInstance $data: D diff --git a/packages/runtime-dom/src/apiCustomElement.ts b/packages/runtime-dom/src/apiCustomElement.ts index 58bc48f64d6..1e551cc05da 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -52,9 +52,9 @@ export function defineCustomElement< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = EmitsOptions, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, - II extends string = string + II extends string = string, + S extends SlotsType = {} >( options: ComponentOptionsWithoutProps< Props, @@ -66,9 +66,9 @@ export function defineCustomElement< Extends, E, EE, - S, I, - II + II, + S > & { styles?: string[] } ): VueElementConstructor @@ -83,9 +83,9 @@ export function defineCustomElement< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = Record, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, - II extends string = string + II extends string = string, + S extends SlotsType = {} >( options: ComponentOptionsWithArrayProps< PropNames, @@ -97,9 +97,9 @@ export function defineCustomElement< Extends, E, EE, - S, I, - II + II, + S > & { styles?: string[] } ): VueElementConstructor<{ [K in PropNames]: any }> @@ -114,9 +114,9 @@ export function defineCustomElement< Extends extends ComponentOptionsMixin = ComponentOptionsMixin, E extends EmitsOptions = Record, EE extends string = string, - S extends SlotsType = {}, I extends ComponentInjectOptions = {}, - II extends string = string + II extends string = string, + S extends SlotsType = {} >( options: ComponentOptionsWithObjectProps< PropsOptions, @@ -128,9 +128,9 @@ export function defineCustomElement< Extends, E, EE, - S, I, - II + II, + S > & { styles?: string[] } ): VueElementConstructor>