JS.VUE.REQUIRE.VALID.DEFAULT.PROP

Enforce props default values to be valid

Rule Details

This rule checks whether the default value of each prop is valid for the given type. It should report an error when default value for type Array or Object is not returned using function.

{'vue/require-valid-default-prop': ['error']}

Copy
<script>
export default {
  props: {
    /* GOOD */
    // basic type check (`null` means accept any type)
    propA: Number,
    // multiple possible types
    propB: [String, Number],
    // a number with default value
    propD: {
      type: Number,
      default: 100
    },
    // object/array defaults should be returned from a factory function
    propE: {
      type: Object,
      default() {
        return { message: 'hello' }
      }
    },
    propF: {
      type: Array,
      default() {
        return []
      }
    },
    /* BAD */
    propA: {
      type: String,
      default: {}
    },
    propB: {
      type: String,
      default: []
    },
    propC: {
      type: Object,
      default: []
    },
    propD: {
      type: Array,
      default: []
    },
    propE: {
      type: Object,
      default: { message: 'hello' }
    }
  }
}
</script>

Options

Nothing.

Further Reading

  • Guide - Prop Validation (https://v3.vuejs.org/guide/component-props.html#prop-validation)

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/