]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix functional component for `h` (#9991)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Tue, 9 Jan 2024 08:45:05 +0000 (16:45 +0800)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 08:45:05 +0000 (16:45 +0800)
- stricter children/slots type
- fix emits/`EE` type argument of `FunctionalComponent`

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

index 62e619c22bddd925b08167dfe1c6cf4a051c9d28..a199e14ced92df7c881512ae455b8af487a984c5 100644 (file)
@@ -2,8 +2,10 @@ import {
   type Component,
   type DefineComponent,
   Fragment,
+  type FunctionalComponent,
   Suspense,
   Teleport,
+  type VNode,
   defineComponent,
   h,
   ref,
@@ -77,6 +79,19 @@ describe('h inference w/ Suspense', () => {
   h(Suspense, { onResolve: 1 })
 })
 
+declare const fc: FunctionalComponent<
+  {
+    foo: string
+    bar?: number
+    onClick: (evt: MouseEvent) => void
+  },
+  ['click'],
+  {
+    default: () => VNode
+    title: (scope: { id: number }) => VNode
+  }
+>
+declare const vnode: VNode
 describe('h inference w/ functional component', () => {
   const Func = (_props: { foo: string; bar?: number }) => ''
   h(Func, { foo: 'hello' })
@@ -87,6 +102,15 @@ describe('h inference w/ functional component', () => {
   h(Func, {})
   //  @ts-expect-error
   h(Func, { bar: 123 })
+
+  h(
+    fc,
+    { foo: 'hello', onClick: () => {} },
+    {
+      default: () => vnode,
+      title: ({ id }: { id: number }) => vnode,
+    },
+  )
 })
 
 describe('h support w/ plain object component', () => {
index d683a288c26d09e3db285167a10fa7f853733f86..93e7fd9bc8805712b0f27ac0586fc4b390fe16ea 100644 (file)
@@ -10,7 +10,7 @@ import {
 } from './vnode'
 import type { Teleport, TeleportProps } from './components/Teleport'
 import type { Suspense, SuspenseProps } from './components/Suspense'
-import { isArray, isObject } from '@vue/shared'
+import { type IfAny, isArray, isObject } from '@vue/shared'
 import type { RawSlots } from './componentSlots'
 import type {
   Component,
@@ -140,11 +140,11 @@ export function h(
 export function h<
   P,
   E extends EmitsOptions = {},
-  S extends Record<string, any> = {},
+  S extends Record<string, any> = any,
 >(
-  type: FunctionalComponent<P, E, S>,
+  type: FunctionalComponent<P, any, S, any>,
   props?: (RawProps & P) | ({} extends P ? null : never),
-  children?: RawChildren | RawSlots,
+  children?: RawChildren | IfAny<S, RawSlots, S>,
 ): VNode
 
 // catch-all for generic component types