I have a Select component that accepts an array of options
. Each option
can be an object of anything as long as it has the following attributes id
and text
So I typed it like this:
type SelectOption<T> = {
id: string | number
text: string
} & T
But I'm not sure how to use this with defineProps
and also defineEmits
in the component.
defineProps<{
options: SelectOption<??>
modelValue: SelectOption<??>
}>()
defineEmits<{
(event: 'update:modelValue', SelectOption<??>): void
}>()
Here is an updated answer to account for the recent improvements in this area :
This allows you to write the following code (from the linked release blog post) :
<script setup lang="ts" generic="T extends string | number, U extends Item">
import type { Item } from './types'
defineProps<{
id: T
list: U[]
}>()
</script>
Of course, this requires you to upgrade to the latest versions of vue and volar.
Note that as of writing this (May 2023), this feature is a bit rough around the edges but is still actively developed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With