Eslint 在定义前不使用单例

Bas*_*Bas 5 typescript eslint typescript-eslint

我最近创建了一个新的存储库,当我创建一个单例类时,我收到no-use-before-define_instance 类型的 eslint 错误。而其他存储库中的相同代码不会给出此错误。代码本身可以工作,我可以禁用该规则,但我想找出为什么会发生这种情况。

export class UserService {
  private static _instance: UserService;

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

    return UserService._instance;
  }

  private constructor() { }
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.json(该规则在此文件中的airbnb-base中声明)

{
  "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"]
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Bas*_*Bas -1

由于某些奇怪的原因,重新启动 eslint 服务器(或 vscode)没有解决任何问题,但重新启动我的笔记本电脑并重新安装节点模块解决了问题......