diff --git a/src/guide/extras/render-function.md b/src/guide/extras/render-function.md index 12659a9889..c2e8b6df96 100644 --- a/src/guide/extras/render-function.md +++ b/src/guide/extras/render-function.md @@ -729,3 +729,78 @@ MyComponent.inheritAttrs = false ``` Functional components can be registered and consumed just like normal components. If you pass a function as the first argument to `h()`, it will be treated as a functional component. + +### Typing Functional Components {#typing-functional-components} + +Functional Components can be typed based on whether they are named or anonymous. Volar also supports type checking properly typed functional components when consuming them in SFC templates. + +**Named Functional Component** + +```tsx +import type { SetupContext } from 'vue' +type FComponentProps = { + message: string +} + +type Events = { + sendMessage(message: string): void +} + +function FComponent( + props: FComponentProps, + context: SetupContext +) { + return ( + + ) +} + +FComponent.props = { + message: { + type: String, + required: true + } +} + +FComponent.emits = { + sendMessage: (value: unknown) => typeof value === 'string' +} +``` + +**Anonymous Functional Component** + +```tsx +import type { FunctionalComponent } from 'vue' + +type FComponentProps = { + message: string +} + +type Events = { + sendMessage(message: string): void +} + +const FComponent: FunctionalComponent = ( + props, + context +) => { + return ( + + ) +} + +FComponent.props = { + message: { + type: String, + required: true + } +} + +FComponent.emits = { + sendMessage: (value) => typeof value === 'string' +} +```