0

Let's say we have two functions that call each other:

const a = () => {
  console.log("a");

  if (Math.random() > 0.9) {
    // Dodging infinite recursion.
    return;
  }

  b(); // 'b' was used before it was defined. (@typescript-eslint/no-use-before-define) eslint
};

const b = () => {
  console.log("b");

  a();
};

First approach in this answer (/* global b */ at top of file) doesn't work for me, while I don't think declaring variable before initialization is possible with const.

So what is the best way to handle this linting error?

Matija Sirk
  • 596
  • 2
  • 15

1 Answers1

1

I think if you declare the variables before assigning the functions you'd be fine. Something like this:

let a: () => void, b: () => void;
a = () => {...}
b = () => {...}

Not sure it is better than just ignoring the rule for the mentioned line or just disabling the rule entirely.

Felix
  • 1,337
  • 10
  • 10
  • I would personally just use an `eslint-disable` comment to ignore the warning and add a comment mentioning that the functions use mutual recursion, eg `// eslint-disable @typescript-eslint/no-use-before-define -- the functions use mutual recursion and thus cannot be ordered to satisfy this rule` – Brad Zacher May 12 '23 at 01:06