Home > Software design >  Nuxt `defineProps` from TypeScript types
Nuxt `defineProps` from TypeScript types

Time:09-29

I'm using the Nuxt 3 / Vue 3 defineProps with TypeScript and want to infer types prop types from TypeScript types.

import { User } from '~/types/types'

const props = defineProps({
  users: {
    type: Array, // User[]?
  },
})

In this example how do I make the users prop a type of User[]?

CodePudding user response:

Use Vue's PropType

import { PropType } from 'vue'
import { User } from '~/types/types'

const props = defineProps = ({
  users: {
    type: Array as PropType<User[]>
  },
})

CodePudding user response:

You could also use a pure-type syntax :

import { User } from '~/types/types'

const props = defineProps<{users:User[]}>()
  • Related