Search code examples
typescripteslinttypescript-eslint

Eslint no-use-before-define with singleton


I recently created a new repo and when I create a singleton class I get a eslint no-use-before-define error on the type of _instance. This while the same code in other repo's does not give this error. The code itself works and I could disable the rule, but I'd like to find out why this is happening.

export class UserService {
  private static _instance: UserService;

  public static get instance(): UserService {
    if (!UserService._instance) {
      UserService._instance = new UserService();
    }

    return UserService._instance;
  }

  private constructor() { }
}

.eslintrc.json (the rule is declared in airbnb-base in this file)

{
  "env": {
    "browser": false,
    "es6": true,
    "node": true
  },
  "extends": ["airbnb-base","airbnb-typescript/base", "prettier"],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly",
    "Express": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "plugins": ["@typescript-eslint", "prettier"],
  "rules": {
    "prettier/prettier": ["error"],
    "class-methods-use-this": "off",
    "no-underscore-dangle": "off",
    "import/prefer-default-export": "off",
    "no-console": "off",
    "no-unused-vars": "off",
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": "error",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "vars": "all",
        "args": "after-used",
        "ignoreRestSiblings": false
      }
    ],
    "no-useless-constructor": "off",
    "@typescript-eslint/no-useless-constructor": "error",
    "no-empty-function": [
      "error",
      {
        "allow": ["constructors"]
      }
    ],
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "ts": "never"
      }
    ],
    "camelcase": "off",
    "import/no-extraneous-dependencies": "off"
  },
  "overrides": [],
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".ts"]
      }
    }
  }
}

Solution

  • For some odd reason restarting the eslint server (or vscode) didn't fix anything, but a restart of my laptop and reinstalling the node modules fixed the issue...