close

prefer-add-event-listener-options

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-add-event-listener-options': 'error',
    },
  },
]);

Rule Details

Prefer the options-object form of .addEventListener() over the legacy boolean useCapture argument.

The object form makes the capture behavior explicit and leaves room for other listener options such as passive, once, and signal.

Examples of incorrect code for this rule:

element.addEventListener('click', handleClick, true);
element.addEventListener('scroll', handleScroll, false);

Examples of correct code for this rule:

element.addEventListener('click', handleClick, { capture: true });
element.addEventListener('scroll', handleScroll, { capture: false });
element.addEventListener('scroll', handleScroll, {
  passive: true,
  capture: true,
});

Original Documentation