Home > front end >  How to set prop with generic type array?
How to set prop with generic type array?

Time:05-22

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 }
})

CodePudding user response:

you can use like this

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

CodePudding user response:

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

  • Related