小编Bas*_*Bas的帖子

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

typescript eslint typescript-eslint

5
推荐指数
1
解决办法
1452
查看次数

标签 统计

eslint ×1

typescript ×1

typescript-eslint ×1