Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no-use-before-define typescript interfaces

I have 2 typescript interfaces that have a member that is of the type of the other interface (circular reference). My linter complains that I cannot use an interface before I defined it. However, I have to define one first.

interface Foo {
  // error, Bar was used before it was defined
  bar: Bar
}

interface Bar {
  foo: Foo
}

I am very much aware that I can just disable the linter rule for the line but what is the proper way of solving it?

like image 564
Fuzzyma Avatar asked Aug 26 '26 01:08

Fuzzyma


1 Answers

With eslint and typescript, there are 2 rules to consider

  • no-use-before-define
  • @typescript-eslint/no-use-before-define

In the docs how to use, it is explained that you must turn off no-use-before-define and enable @typescript-eslint/no-use-before-define.

This made the error go away for me.

// eslint.js

module.exports = {
  "rules": {
    // Note: you must disable the base rule as it can report incorrect errors
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": "error"
  }
}
like image 131
Kev Avatar answered Aug 29 '26 21:08

Kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!