Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/guide/components/slots.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ Notice how `v-slot="receivedProps"` matches the slot function signature. Just li
</ChildComponent>
```

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:
Expand Down
21 changes: 21 additions & 0 deletions src/guide/typescript/options-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ export default defineComponent({
})
```

## Typing Component Slots {#typing-component-slots}

Slots can be typed with the `slots` option <sup class="vt-badge" data-text="3.3+" /> 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 `<script setup>`, each key maps to the slot's props object, not a function. This gives type checking for `this.$slots` and for slot props in parent templates. See the [`slots` option](/api/options-rendering#slots) for more details.

## Typing Computed Properties {#typing-computed-properties}

A computed property infers its type based on its return value:
Expand Down