]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): improve `h` overload to support union of string and component (#5432)
authorCarlos Rodrigues <carlos@hypermob.co.uk>
Mon, 23 Oct 2023 15:40:06 +0000 (16:40 +0100)
committerGitHub <noreply@github.com>
Mon, 23 Oct 2023 15:40:06 +0000 (23:40 +0800)
fix #5431

packages/dts-test/h.test-d.ts
packages/runtime-core/src/h.ts

index 5c700800e94a30c60fed740ca2c079e690ccd5ec..f2e984b49b8d4faf939a543edefd9415c8ebeab5 100644 (file)
@@ -1,6 +1,7 @@
 import {
   h,
   defineComponent,
+  DefineComponent,
   ref,
   Fragment,
   Teleport,
@@ -231,3 +232,18 @@ describe('resolveComponent should work', () => {
     message: '1'
   })
 })
+
+// #5431
+describe('h should work with multiple types', () => {
+  const serializers = {
+    Paragraph: 'p',
+    Component: {} as Component,
+    DefineComponent: {} as DefineComponent
+  }
+
+  const sampleComponent = serializers['' as keyof typeof serializers]
+
+  h(sampleComponent)
+  h(sampleComponent, {})
+  h(sampleComponent, {}, [])
+})
index 73b27107b8bb006133f11b462d7ee19287147e73..4ca90262f2a95dcf445b472766f3af5df5cc0e54 100644 (file)
@@ -174,6 +174,14 @@ export function h<P>(
   children?: RawChildren | RawSlots
 ): VNode
 
+// catch all types
+export function h(type: string | Component, children?: RawChildren): VNode
+export function h<P>(
+  type: string | Component<P>,
+  props?: (RawProps & P) | ({} extends P ? null : never),
+  children?: RawChildren | RawSlots
+): VNode
+
 // Actual implementation
 export function h(type: any, propsOrChildren?: any, children?: any): VNode {
   const l = arguments.length