Skip to content

Commit e94b01b

Browse files
authored
fix(types/custome-element): defineCustomElement props inference with array emits (#11384)
close #11353
1 parent a01675e commit e94b01b

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

packages/dts-test/defineCustomElement.test-d.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('inject', () => {
6868
})
6969

7070
describe('defineCustomElement using defineComponent return type', () => {
71-
test('with emits', () => {
71+
test('with object emits', () => {
7272
const Comp1Vue = defineComponent({
7373
props: {
7474
a: String,
@@ -80,6 +80,23 @@ describe('defineCustomElement using defineComponent return type', () => {
8080
const Comp = defineCustomElement(Comp1Vue)
8181
expectType<VueElementConstructor>(Comp)
8282

83-
expectType<string | undefined>(new Comp().a)
83+
const instance = new Comp()
84+
expectType<string | undefined>(instance.a)
85+
instance.a = ''
86+
})
87+
88+
test('with array emits', () => {
89+
const Comp1Vue = defineComponent({
90+
props: {
91+
a: Number,
92+
},
93+
emits: ['click'],
94+
})
95+
const Comp = defineCustomElement(Comp1Vue)
96+
expectType<VueElementConstructor>(Comp)
97+
98+
const instance = new Comp()
99+
expectType<number | undefined>(instance.a)
100+
instance.a = 42
84101
})
85102
})

packages/runtime-dom/src/apiCustomElement.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,17 @@ export function defineCustomElement<
142142
>,
143143
): VueElementConstructor<ResolvedProps>
144144

145-
// overload 5: defining a custom element from the returned value of
145+
// overload 3: defining a custom element from the returned value of
146146
// `defineComponent`
147-
export function defineCustomElement<P>(
148-
options: DefineComponent<P, any, any, any>,
149-
): VueElementConstructor<ExtractPropTypes<P>>
147+
export function defineCustomElement<
148+
T extends DefineComponent<any, any, any, any>,
149+
>(
150+
options: T,
151+
): VueElementConstructor<
152+
T extends DefineComponent<infer P, any, any, any>
153+
? ExtractPropTypes<P>
154+
: unknown
155+
>
150156

151157
/*! #__NO_SIDE_EFFECTS__ */
152158
export function defineCustomElement(

0 commit comments

Comments
 (0)