Skip to content

Commit f44feed

Browse files
authored
fix(types): avoid merging component instance into $props in ComponentInstance (#12870)
close #12751
1 parent c69c4bb commit f44feed

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

packages-private/dts-test/componentInstance.test-d.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,18 @@ describe('Generic component', () => {
137137
expectType<string | number>(comp.msg)
138138
expectType<Array<string | number>>(comp.list)
139139
})
140+
141+
// #12751
142+
{
143+
const Comp = defineComponent({
144+
__typeEmits: {} as {
145+
'update:visible': [value?: boolean]
146+
},
147+
})
148+
const comp: ComponentInstance<typeof Comp> = {} as any
149+
150+
expectType<((value?: boolean) => any) | undefined>(comp['onUpdate:visible'])
151+
expectType<{ 'onUpdate:visible'?: (value?: boolean) => any }>(comp['$props'])
152+
// @ts-expect-error
153+
comp['$props']['$props']
154+
}

packages/runtime-core/src/component.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,23 @@ export type ComponentInstance<T> = T extends { new (): ComponentPublicInstance }
115115
: T extends FunctionalComponent<infer Props, infer Emits>
116116
? ComponentPublicInstance<Props, {}, {}, {}, {}, ShortEmitsToObject<Emits>>
117117
: T extends Component<
118-
infer Props,
118+
infer PropsOrInstance,
119119
infer RawBindings,
120120
infer D,
121121
infer C,
122122
infer M
123123
>
124-
? // NOTE we override Props/RawBindings/D to make sure is not `unknown`
125-
ComponentPublicInstance<
126-
unknown extends Props ? {} : Props,
127-
unknown extends RawBindings ? {} : RawBindings,
128-
unknown extends D ? {} : D,
129-
C,
130-
M
131-
>
124+
? PropsOrInstance extends { $props: unknown }
125+
? // T is returned by `defineComponent()`
126+
PropsOrInstance
127+
: // NOTE we override Props/RawBindings/D to make sure is not `unknown`
128+
ComponentPublicInstance<
129+
unknown extends PropsOrInstance ? {} : PropsOrInstance,
130+
unknown extends RawBindings ? {} : RawBindings,
131+
unknown extends D ? {} : D,
132+
C,
133+
M
134+
>
132135
: never // not a vue Component
133136

134137
/**
@@ -259,16 +262,16 @@ export type ConcreteComponent<
259262
* The constructor type is an artificial type returned by defineComponent().
260263
*/
261264
export type Component<
262-
Props = any,
265+
PropsOrInstance = any,
263266
RawBindings = any,
264267
D = any,
265268
C extends ComputedOptions = ComputedOptions,
266269
M extends MethodOptions = MethodOptions,
267270
E extends EmitsOptions | Record<string, any[]> = {},
268271
S extends Record<string, any> = any,
269272
> =
270-
| ConcreteComponent<Props, RawBindings, D, C, M, E, S>
271-
| ComponentPublicInstanceConstructor<Props>
273+
| ConcreteComponent<PropsOrInstance, RawBindings, D, C, M, E, S>
274+
| ComponentPublicInstanceConstructor<PropsOrInstance>
272275

273276
export type { ComponentOptions }
274277

0 commit comments

Comments
 (0)