From: Evan You Date: Tue, 30 Apr 2024 17:09:06 +0000 (-0700) Subject: fix(types): fix compat with generated types that rely on CreateComponentPublicInstance X-Git-Tag: v3.5.0-alpha.2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c146186396d0c1a65423b8c9a21251c5a6467336;p=thirdparty%2Fvuejs%2Fcore.git fix(types): fix compat with generated types that rely on CreateComponentPublicInstance close #10842 --- diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 077f1abc07..aa0cfb0eba 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1766,3 +1766,196 @@ defineComponent({ expectType(props.foo) }, }) + +import type * as vue from 'vue' + +interface ErrorMessageSlotProps { + message: string | undefined +} +/** + * #10842 + * component types generated by vue-tsc + * relying on legacy CreateComponentPublicInstance signature + */ +declare const ErrorMessage: { + new (...args: any[]): vue.CreateComponentPublicInstance< + Readonly< + vue.ExtractPropTypes<{ + as: { + type: StringConstructor + default: any + } + name: { + type: StringConstructor + required: true + } + }> + >, + () => + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + > + | vue.Slot + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + | { + default: () => VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + }, + unknown, + {}, + {}, + vue.ComponentOptionsMixin, + vue.ComponentOptionsMixin, + {}, + vue.VNodeProps & + vue.AllowedComponentProps & + vue.ComponentCustomProps & + Readonly< + vue.ExtractPropTypes<{ + as: { + type: StringConstructor + default: any + } + name: { + type: StringConstructor + required: true + } + }> + >, + { + as: string + }, + true, + {}, + {}, + { + P: {} + B: {} + D: {} + C: {} + M: {} + Defaults: {} + }, + Readonly< + vue.ExtractPropTypes<{ + as: { + type: StringConstructor + default: any + } + name: { + type: StringConstructor + required: true + } + }> + >, + () => + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + > + | vue.Slot + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + | { + default: () => VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + }, + {}, + {}, + {}, + { + as: string + } + > + __isFragment?: never + __isTeleport?: never + __isSuspense?: never +} & vue.ComponentOptionsBase< + Readonly< + vue.ExtractPropTypes<{ + as: { + type: StringConstructor + default: any + } + name: { + type: StringConstructor + required: true + } + }> + >, + () => + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + > + | vue.Slot + | VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + | { + default: () => VNode< + vue.RendererNode, + vue.RendererElement, + { + [key: string]: any + } + >[] + }, + unknown, + {}, + {}, + vue.ComponentOptionsMixin, + vue.ComponentOptionsMixin, + {}, + string, + { + as: string + }, + {}, + string, + {} +> & + vue.VNodeProps & + vue.AllowedComponentProps & + vue.ComponentCustomProps & + (new () => { + $slots: { + default: (arg: ErrorMessageSlotProps) => VNode[] + } + }) +; diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 7fce96586d..98e9ae7952 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -31,7 +31,7 @@ import { extend, isFunction } from '@vue/shared' import type { VNodeProps } from './vnode' import type { ComponentPublicInstanceConstructor, - CreateComponentPublicInstance, + CreateComponentPublicInstanceWithMixins, } from './componentPublicInstance' import type { SlotsType } from './componentSlots' import type { Directive } from './directives' @@ -68,7 +68,7 @@ export type DefineComponent< Provide extends ComponentProvideOptions = ComponentProvideOptions, MakeDefaultsOptional extends boolean = true, > = ComponentPublicInstanceConstructor< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< Props, RawBindings, D, @@ -116,7 +116,7 @@ export type DefineSetupFnComponent< PP = PublicProps, > = new ( props: Props & PP, -) => CreateComponentPublicInstance< +) => CreateComponentPublicInstanceWithMixins< Props, {}, {}, @@ -240,7 +240,7 @@ export function defineComponent< Provide > & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< ResolvedProps, SetupBindings, Data, diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index ac1841edee..2ede440426 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -62,7 +62,7 @@ import type { import type { Directive } from './directives' import { type ComponentPublicInstance, - type CreateComponentPublicInstance, + type CreateComponentPublicInstanceWithMixins, type IntersectionMixin, type UnwrapMixinsType, isReservedPrefix, @@ -263,7 +263,7 @@ export type ComponentOptions< Provide > & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< {}, RawBindings, D, @@ -372,7 +372,7 @@ interface LegacyOptions< // since that leads to some sort of circular inference and breaks ThisType // for the entire component. data?: ( - this: CreateComponentPublicInstance< + this: CreateComponentPublicInstanceWithMixins< Props, {}, {}, @@ -381,7 +381,7 @@ interface LegacyOptions< Mixin, Extends >, - vm: CreateComponentPublicInstance< + vm: CreateComponentPublicInstanceWithMixins< Props, {}, {}, @@ -1125,7 +1125,7 @@ export type ComponentOptionsWithoutProps< */ __typeEmits?: TE } & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< PE, RawBindings, D, @@ -1187,7 +1187,7 @@ export type ComponentOptionsWithArrayProps< > & { props: PropNames[] } & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< Props, RawBindings, D, @@ -1250,7 +1250,7 @@ export type ComponentOptionsWithObjectProps< > & { props: PropsOptions & ThisType } & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< Props, RawBindings, D, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 864b9786ef..91a7ae8d6d 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -150,7 +150,71 @@ export type ComponentPublicInstanceConstructor< new (...args: any[]): T } +/** + * @deprecated This is no longer used internally, but exported and relied on by + * existing library types generated by vue-tsc. + */ export type CreateComponentPublicInstance< + P = {}, + B = {}, + D = {}, + C extends ComputedOptions = {}, + M extends MethodOptions = {}, + Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, + Extends extends ComponentOptionsMixin = ComponentOptionsMixin, + E extends EmitsOptions = {}, + PublicProps = P, + Defaults = {}, + MakeDefaultsOptional extends boolean = false, + I extends ComponentInjectOptions = {}, + S extends SlotsType = {}, + PublicMixin = IntersectionMixin & IntersectionMixin, + PublicP = UnwrapMixinsType & EnsureNonVoid

, + PublicB = UnwrapMixinsType & EnsureNonVoid, + PublicD = UnwrapMixinsType & EnsureNonVoid, + PublicC extends ComputedOptions = UnwrapMixinsType & + EnsureNonVoid, + PublicM extends MethodOptions = UnwrapMixinsType & + EnsureNonVoid, + PublicDefaults = UnwrapMixinsType & + EnsureNonVoid, +> = ComponentPublicInstance< + PublicP, + PublicB, + PublicD, + PublicC, + PublicM, + E, + PublicProps, + PublicDefaults, + MakeDefaultsOptional, + ComponentOptionsBase< + P, + B, + D, + C, + M, + Mixin, + Extends, + E, + string, + Defaults, + {}, + string, + S + >, + I, + S +> + +/** + * This is the same as `CreateComponentPublicInstance` but adds local components, + * global directives, exposed, and provide inference. + * It changes the arguments order so that we don't need to repeat mixin + * inference everywhere internally, but it has to be a new type to avoid + * breaking types that relies on previous arguments order (#10842) + */ +export type CreateComponentPublicInstanceWithMixins< P = {}, B = {}, D = {}, @@ -167,6 +231,8 @@ export type CreateComponentPublicInstance< LC extends Record = {}, Directives extends Record = {}, Exposed extends string = string, + Provide extends ComponentProvideOptions = ComponentProvideOptions, + // mixin inference PublicMixin = IntersectionMixin & IntersectionMixin, PublicP = UnwrapMixinsType & EnsureNonVoid

, PublicB = UnwrapMixinsType & EnsureNonVoid, @@ -177,7 +243,6 @@ export type CreateComponentPublicInstance< EnsureNonVoid, PublicDefaults = UnwrapMixinsType & EnsureNonVoid, - Provide extends ComponentProvideOptions = ComponentProvideOptions, > = ComponentPublicInstance< PublicP, PublicB, diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index e4a9e53f29..abdd39a9cd 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -279,6 +279,7 @@ export type { ComponentPublicInstance, ComponentCustomProperties, CreateComponentPublicInstance, + CreateComponentPublicInstanceWithMixins, } from './componentPublicInstance' export type { Renderer, diff --git a/packages/runtime-dom/src/apiCustomElement.ts b/packages/runtime-dom/src/apiCustomElement.ts index 0172846624..6363f16de7 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -9,7 +9,7 @@ import { type ComponentProvideOptions, type ComputedOptions, type ConcreteComponent, - type CreateComponentPublicInstance, + type CreateComponentPublicInstanceWithMixins, type DefineComponent, type Directive, type EmitsOptions, @@ -97,7 +97,7 @@ export function defineCustomElement< Provide > & ThisType< - CreateComponentPublicInstance< + CreateComponentPublicInstanceWithMixins< Readonly, SetupBindings, Data,