Vue3 Fundamentals · Vue 3 + TypeScript

Typing slots

Typed slots

Typing slots

A slot lets a parent fill in part of a child's markup. A scoped slot also passes data back out, and in a <script setup lang="ts"> block you describe that data with defineSlots<…>().

<script setup lang="ts">
defineSlots<{ default(props: { item: string }): any }>();

const item = 'Vue';
</script>

<template>
  <div><slot :item="item" /></div>
</template>

The type says the default slot receives a scope with an item of type string. The template passes it with :item="item". The parent's slot content reads props.item, and the type checks that use. Without the declaration the scope is any, and a typo in the parent goes unnoticed until the slot renders.

defineSlots is compile-time only: Vue does not build a runtime declaration from it. It exists so the parent and the child agree on the slot's scope. The declaration is documentation the compiler enforces.

In App.vue, expose an item to the default slot.

Challenge

The slot renders nothing because it passes no data. Export an App component from App.vue that declares a default slot with the type-only defineSlots<{ default(props: { item: string }): any }>() form and passes :item="item" to it.

The Test fills the slot with a function that renders the scope's item. The starter renders a bare slot, so the scope is empty and the Test fails.

App.vue
<script setup lang="ts">
defineSlots<{ default(props: { item: string }): any }>();

const item = 'Vue';
</script>

<template>
  <div><slot /></div>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.