Skip to content

useVueSingleEventPayload (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useVueSingleEventPayload": "error"
}
}
}
}

Require Vue component events to carry at most one value.

A component can send a value along with an event, as in emit('change', value), and it can send several, as in emit('change', value, extra). A parent that listens to the event with a handler written inline in its template, such as @change="onChange($event)", can only read the first value through $event. The other values are silently lost.

Put all the values in one object instead, as in emit('change', { value, extra }), so every listener receives all of them.

This rule checks the calls that emit an event: $emit() in templates, this.$emit(), the emit function that setup() receives, and the function returned by defineEmits(). It also checks the events declared in the emits option and in defineEmits(), when the declaration lists the values the event carries.

Calls whose event name is not written as a string, as in emit(eventName, a, b), are not checked.

<script setup>
const emit = defineEmits(["change"]);
function onInput(value, extra) {
emit("change", value, extra);
}
</script>
code-block.vue:5:25 lint/nursery/useVueSingleEventPayload ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The event change is emitted with more than one value.

4 │ function onInput(value, extra) {
> 5 │ emit("change", value, extra);
│ ^^^^^
6 │ }
7 │ </script>

ℹ A listener written inline in a template, such as @change="onChange($event)", can only read the first value through $event. The other values are lost.

ℹ Put all the values in one object, and send that object instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script>
export default {
emits: {
change: (value, extra) => value !== null,
},
};
</script>
code-block.vue:4:21 lint/nursery/useVueSingleEventPayload ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The event change is declared with more than one value.

2 │ export default {
3 │ emits: {
> 4 │ change: (value, extra) => value !== null,
│ ^^^^^
5 │ },
6 │ };

ℹ A listener written inline in a template, such as @change="onChange($event)", can only read the first value through $event. The other values are lost.

ℹ Put all the values in one object, and send that object instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script setup lang="ts">
defineEmits<{
change: [value: string, extra: number];
}>();
</script>
code-block.vue:3:27 lint/nursery/useVueSingleEventPayload ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The event change is declared with more than one value.

1 │ <script setup lang="ts">
2 │ defineEmits<{
> 3 │ change: [value: string, extra: number];
│ ^^^^^^^^^^^^^
4 │ }>();
5 │ </script>

ℹ A listener written inline in a template, such as @change="onChange($event)", can only read the first value through $event. The other values are lost.

ℹ Put all the values in one object, and send that object instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script setup lang="ts">
const emit = defineEmits<{
change: [payload: { value: string; extra: number }];
close: [];
}>();
function onInput(value: string, extra: number) {
emit("change", { value, extra });
}
function onClose() {
emit("close");
}
</script>