目录

@typescript-eslint/no-loop-func

Disallow function declarations that contain unsafe references inside loop statements.

This rule applies only to .js and .ts files.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/no-loop-func": "error"
  }
}

Options

This rule is not configurable.

Example of Correct Code

const a = function(): void {
  console.info('hello');
};

for (let i = 10; i; i--) {
  a();
}

for (let i = 10; i; i--) {
  const b = function(): void {
    a();
  }; // OK, no references to variables in the outer scopes.
  b();
}

Example of Incorrect Code

const num = 10;
for (let i = num; i; i--) {
  // Variable i is an unsafe reference.
  (function(): number {
    return i;
  })();
}

let i1 = 0;
while (i1 < num) {
  // Variable i is an unsafe reference.
  const a = function(): number {
    return i1;
  };
  a();

  i1++;
}

let i2 = 0;
do {
  // Variable i is an unsafe reference.
  function a(): number {
    return i2;
  }
  a();

  i2++;
} while (i2 < num);

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.