close

prefer-arrow-callback

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'prefer-arrow-callback': 'error',
    },
  },
]);

Rule Details

Requires using arrow functions for callbacks when the function expression can be replaced without changing this, super, arguments, or new.target semantics.

Examples of incorrect code for this rule:

foo(function (value) {
  return value;
});

foo(function () {
  return this.value;
}.bind(this));

Examples of correct code for this rule:

foo((value) => {
  return value;
});

foo(() => {
  return this.value;
});

foo(function () {
  return this.value;
});

Options

This rule accepts an options object with the following properties:

  • allowNamedFunctions defaults to false. When true, named function expressions are allowed.
  • allowUnboundThis defaults to true. When false, callbacks that reference their own this are still reported, but they are not automatically fixed.

Examples of correct code for this rule with { "allowNamedFunctions": true }:

{ "prefer-arrow-callback": ["error", { "allowNamedFunctions": true }] }
foo(function namedCallback() {});

Examples of incorrect code for this rule with { "allowUnboundThis": false }:

{ "prefer-arrow-callback": ["error", { "allowUnboundThis": false }] }
foo(function () {
  return this.value;
});

Original Documentation