JavaScript

JavaScript

Made by DeepSource

emits validator does not always return a boolean value JS-0660

Bug risk
Major
vue

Emitted events can be defined on the component via the emits option. In the event a native event (e.g., click) is defined in the emits option, it will be overwritten by the event in the component instead of being treated as a native listener. To add validation, the event is assigned a function that receives the arguments passed to the $emit call and returns a boolean to indicate whether the event is valid or not.

Bad Practice


<script>
export default {
  emits: {
    qux: function () {},
    quux (evt) {
      if (!evt) {
        return false
      }
    }
  }
}
</script>


Recommended

<script>
export default {
  emits: {
    foo (evt) {
      if (evt) {
        return true
      } else {
        return false
      }
    },
    bar: function () {
      return true
    },
    baz (evt) {
      if (evt) {
        return true
      }
    },
  }
}
</script>