Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set prop with generic type array?

I have a select component and what I am trying to do is to implement the component in that way so I can pass an array of any type. How can this be done? Where should the generic be defined? I am using <script setup> with typescript.

This is how I tried to define the props:

const props = defineProps<T[]>({
    options: {type: Array as PropType<T[]>, required: true }
})
like image 712
David Gheeorghe Avatar asked Dec 11 '25 04:12

David Gheeorghe


1 Answers

Since you're using TypeScript, Vue allows you to do this:

const props = defineProps<{
    options: T[];
}>();

https://vuejs.org/api/sfc-script-setup.html#typescript-only-features

like image 55
catgirlkelly Avatar answered Dec 14 '25 04:12

catgirlkelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!