Typing Vue Slots with TypeScript: defineSlots<T>() for Type-Safe Scoped Slots
AI generated
{ }
Vue.js · defineSlots · TypeScript · Scoped Slots
Typing Vue Slots
with TypeScript: defineSlots() for type-safe scoped slots

A scoped slot carries data from a child component back into a parent's template, yet without explicit typing TypeScript long treated that data as plain any. A typo like item.naem instead of item.name would only surface in the browser, never at compile time. defineSlots() closes exactly that gap: every slot gets a real TypeScript type for its scoped slot props, complete with full autocompletion right in the template of the consuming component, with no additional runtime validation involved.

14 min read defineSlots() · Scoped slots Vue 3.3+ · TypeScript · Volar

1. The problem with untyped slots: a long-standing type gap

While props have long been fully typeable through defineProps(), scoped slots remained the last major gap in TypeScript coverage for Vue components for a long time. A component passing data to its parent through could correctly deliver that data at runtime, yet in the consuming component's template, item and index inside