diff --git a/src/guide/components/slots.md b/src/guide/components/slots.md index 4e2bfa9625..d042e654bf 100644 --- a/src/guide/components/slots.md +++ b/src/guide/components/slots.md @@ -417,6 +417,8 @@ Notice how `v-slot="receivedProps"` matches the slot function signature. Just li ``` +If you're using TypeScript, see Typing Component Slots for the [Composition API](/guide/typescript/composition-api#typing-component-slots) and the [Options API](/guide/typescript/options-api#typing-component-slots). + ### Named Scoped Slots {#named-scoped-slots} Named scoped slots work similarly - slot props are accessible as the value of the `v-slot` directive: `v-slot:name="receivedProps"`. When using the shorthand, it looks like this: diff --git a/src/guide/typescript/options-api.md b/src/guide/typescript/options-api.md index 0bd343884b..686cab031e 100644 --- a/src/guide/typescript/options-api.md +++ b/src/guide/typescript/options-api.md @@ -120,6 +120,27 @@ export default defineComponent({ }) ``` +## Typing Component Slots {#typing-component-slots} + +Slots can be typed with the `slots` option together with the `SlotsType` helper. The option's runtime value is not used, only its type: + +```ts +import { defineComponent } from 'vue' +import type { SlotsType } from 'vue' + +export default defineComponent({ + slots: Object as SlotsType<{ + default: { msg: string } + item: { id: number } + }>, + mounted() { + this.$slots.item // type: ((props: { id: number }) => any) | undefined + } +}) +``` + +Unlike `defineSlots()` in `