Home > front end >  Vue 3 defineProps with Types and ComponentObjectPropsOptions (like default or validator)
Vue 3 defineProps with Types and ComponentObjectPropsOptions (like default or validator)

Time:02-10

From within a setup method, using defineProps I can use

const props = defineProps<{tabs: Tab[]}> = ()

which allows me to have the type Tab[] on props.tabs

however, if I want to specify ComponentObjectPropsOptions, I believe the syntax is

const props = defineProps = ({
  type: Array, //can not use Tab[] here
  required: true,
  validator: ...
})

but with that syntax I lose my type on props.tabs :(

CodePudding user response:

You'll need to use the PropType utility to type cast your array.

https://vuejs.org/api/utility-types.html#proptype-t

const props = defineProps = ({
  type: Array as PropType<Tab[]>,
  required: true,
  validator: ...
})

  • Related