Skip to content

Commit

Permalink
fix(types/custome-element): defineCustomElement props inference wit…
Browse files Browse the repository at this point in the history
…h array emits (#11384)

close #11353
  • Loading branch information
andylizi authored Aug 5, 2024
1 parent a01675e commit e94b01b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
21 changes: 19 additions & 2 deletions packages/dts-test/defineCustomElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('inject', () => {
})

describe('defineCustomElement using defineComponent return type', () => {
test('with emits', () => {
test('with object emits', () => {
const Comp1Vue = defineComponent({
props: {
a: String,
Expand All @@ -80,6 +80,23 @@ describe('defineCustomElement using defineComponent return type', () => {
const Comp = defineCustomElement(Comp1Vue)
expectType<VueElementConstructor>(Comp)

expectType<string | undefined>(new Comp().a)
const instance = new Comp()
expectType<string | undefined>(instance.a)
instance.a = ''
})

test('with array emits', () => {
const Comp1Vue = defineComponent({
props: {
a: Number,
},
emits: ['click'],
})
const Comp = defineCustomElement(Comp1Vue)
expectType<VueElementConstructor>(Comp)

const instance = new Comp()
expectType<number | undefined>(instance.a)
instance.a = 42
})
})
14 changes: 10 additions & 4 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ export function defineCustomElement<
>,
): VueElementConstructor<ResolvedProps>

// overload 5: defining a custom element from the returned value of
// overload 3: defining a custom element from the returned value of
// `defineComponent`
export function defineCustomElement<P>(
options: DefineComponent<P, any, any, any>,
): VueElementConstructor<ExtractPropTypes<P>>
export function defineCustomElement<
T extends DefineComponent<any, any, any, any>,
>(
options: T,
): VueElementConstructor<
T extends DefineComponent<infer P, any, any, any>
? ExtractPropTypes<P>
: unknown
>

/*! #__NO_SIDE_EFFECTS__ */
export function defineCustomElement(
Expand Down

0 comments on commit e94b01b

Please sign in to comment.