Home > Net >  Vetur, type inference of deconstructed props has extra undefined type?
Vetur, type inference of deconstructed props has extra undefined type?

Time:03-01

With the following code, Vetur inferenced x,y to have type number | undefined.

The undefined cause tons of warning on further usage of x,y .

How can I remove the undefined from the type inference?

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: { x: Number, y: Number },
  setup(props) {
    let { x, y } = props
  },
})
</script>

CodePudding user response:

All props are optional (as in it's possibly undefined) by default, so your props are typed as number | undefined.

You can declare the props as required using the long form of prop declaration, which removes | undefined from the prop's type:

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    x: {
      type: Number,
      required: true, //           
  • Related