LoginSignup
2
2

More than 5 years have passed since last update.

メモ Vue.extendでpropsのObjectとArrayに型をつける

Last updated at Posted at 2019-04-29

vue-property-decoratoを使わずにpropsに型をつけたかった際のメモ

Object

<script lang="ts">
import Vue, { PropType } from 'vue'

type Pet = {
  name: string
}

export default Vue.extend({
  props: {
    pet: {
      type: Object as PropType<Pet>,
      required: true
    }
  },
  mounted() {
    // Type Pet
    console.log(this.pet) 
  }
})
</script>

Array

<script lang="ts">
import Vue, { PropType } from 'vue'

type Pet = {
  name: string
}

export default Vue.extend({
  props: {
    pets: {
      type: Array as PropType<Pet[]>,
      required: true
    }
  },
  mounted() {
    // Type Pet[]
    console.log(this.pets)
  }
})
</script>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2