Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set types to vue slot props Typescript

I'm trying to set types on my slot props to handle in a table component as you can see in the image

enter image description here

I also have been trying with

#body={item: UserItem}, but it is only rename the parametter. #body={<UserItem>item} and #body={item<UserItem>}, but it does not work

like image 339
Mauricio Loya Avatar asked Nov 18 '25 20:11

Mauricio Loya


1 Answers

The slot scope is an object so you need to type the object itself like

<template #body="{ item }: { item: UserItem }" />

But if the Table component is your own component you could also use generic in combination with defineProps and defineSlots to automatically infer the type for item in your slot based on the values prop that is passed in.

<script setup lang="ts" generic="T">
const props = defineProps<{
  values: T[]
}>()

const slots = defineSlots<{
  body(props: { item: T }): void
}>()
</script>
like image 158
Craig Harshbarger Avatar answered Nov 21 '25 09:11

Craig Harshbarger