In my Vue3 project, I have a component that accepts a prop which can be either a string or an object. Here's how it looks:
import { defineComponent } from 'vue'
const Component = defineComponent({
props: {
book: {
type: [String, Object]
}
}
})
If the prop is an object, I want to specify a type for it as shown in the Vue3 documentation:
import { defineComponent, PropType } from 'vue'
interface Book {
title: string
year?: number
}
const Component = defineComponent({
props: {
book: {
type: Object as PropType<Book>
}
}
})
My challenge is how to combine both scenarios, like this (which currently doesn't work):
import { defineComponent, PropType } from 'vue'
interface Book {
title: string
year?: number
}
const Component = defineComponent({
props: {
book: {
type: [String, Object as PropType<Book>]
}
}
})