]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types/custome-element): `defineCustomElement` props inference with array emits...
authorAndy Li <andylizi666@gmail.com>
Mon, 5 Aug 2024 03:14:36 +0000 (11:14 +0800)
committerGitHub <noreply@github.com>
Mon, 5 Aug 2024 03:14:36 +0000 (11:14 +0800)
close #11353

packages/dts-test/defineCustomElement.test-d.ts
packages/runtime-dom/src/apiCustomElement.ts

index 2d48dbc1bc9ee5756501b983db12164c954322da..b81c0befe9fd960e669483539ce334625a40f56e 100644 (file)
@@ -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,
@@ -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
   })
 })
index a92248dd55db7e33d6c217cf7c107e496bb67245..add209257460c0cc4da67380776db3cd723f076d 100644 (file)
@@ -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(