From: Tycho Date: Tue, 20 May 2025 00:44:35 +0000 (+0800) Subject: fix(types): avoid merging component instance into `$props` in `ComponentInstance... X-Git-Tag: v3.5.15~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f44feed6fa461a9c4c724e9631c19e9e214c0a20;p=thirdparty%2Fvuejs%2Fcore.git fix(types): avoid merging component instance into `$props` in `ComponentInstance` (#12870) close #12751 --- diff --git a/packages-private/dts-test/componentInstance.test-d.tsx b/packages-private/dts-test/componentInstance.test-d.tsx index a804bb10d5..c6911d3a8c 100644 --- a/packages-private/dts-test/componentInstance.test-d.tsx +++ b/packages-private/dts-test/componentInstance.test-d.tsx @@ -137,3 +137,18 @@ describe('Generic component', () => { expectType(comp.msg) expectType>(comp.list) }) + +// #12751 +{ + const Comp = defineComponent({ + __typeEmits: {} as { + 'update:visible': [value?: boolean] + }, + }) + const comp: ComponentInstance = {} as any + + expectType<((value?: boolean) => any) | undefined>(comp['onUpdate:visible']) + expectType<{ 'onUpdate:visible'?: (value?: boolean) => any }>(comp['$props']) + // @ts-expect-error + comp['$props']['$props'] +} diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 5b094a0d61..f191c36df1 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -115,20 +115,23 @@ export type ComponentInstance = T extends { new (): ComponentPublicInstance } : T extends FunctionalComponent ? ComponentPublicInstance> : T extends Component< - infer Props, + infer PropsOrInstance, infer RawBindings, infer D, infer C, infer M > - ? // NOTE we override Props/RawBindings/D to make sure is not `unknown` - ComponentPublicInstance< - unknown extends Props ? {} : Props, - unknown extends RawBindings ? {} : RawBindings, - unknown extends D ? {} : D, - C, - M - > + ? PropsOrInstance extends { $props: unknown } + ? // T is returned by `defineComponent()` + PropsOrInstance + : // NOTE we override Props/RawBindings/D to make sure is not `unknown` + ComponentPublicInstance< + unknown extends PropsOrInstance ? {} : PropsOrInstance, + unknown extends RawBindings ? {} : RawBindings, + unknown extends D ? {} : D, + C, + M + > : never // not a vue Component /** @@ -259,7 +262,7 @@ export type ConcreteComponent< * The constructor type is an artificial type returned by defineComponent(). */ export type Component< - Props = any, + PropsOrInstance = any, RawBindings = any, D = any, C extends ComputedOptions = ComputedOptions, @@ -267,8 +270,8 @@ export type Component< E extends EmitsOptions | Record = {}, S extends Record = any, > = - | ConcreteComponent - | ComponentPublicInstanceConstructor + | ConcreteComponent + | ComponentPublicInstanceConstructor export type { ComponentOptions }