检测到依赖循环.eslint 与 typeorm

gab*_*el 5 typescript typeorm

我有两个一对一双向的实体类型:

部门:

@Entity('Departament')
export default class Departament {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  departament_name: string;

  @OneToOne(type => User, user => user.departament)
  @JoinColumn()
  user: User;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  UpdatedAt: Date;
}
Run Code Online (Sandbox Code Playgroud)

用户:

@Entity('User')
export default class User {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  name: string;

  @Column()
  last_name: string;

  @Column()
  email: string;

  @Column()
  login: string;

  @Column()
  password: string;

  @OneToOne(type => Departament, departament => departament.user)
  departament: Departament;
}
Run Code Online (Sandbox Code Playgroud)

这些是我的.eslintrc设置:

{
  "env": {
    "es6": true,
    "node": true,
    "jest": true
  },
  "extends": [
    "airbnb-base",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint", "prettier"],
  "rules": {
    "prettier/prettier": "error",
    "no-new": "off",
    "no-underscore-dangle": "off",
    "class-methods-use-this": "off",
    "no-await-in-loop": "off",
    "import/prefer-default-export": "off",
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never"
      }
    ],
    "import/no-extraneous-dependencies": [
      "error",
      {
        "devDependencies": ["**/*.spec.ts", "src/utils/tests/*.ts"]
      }
    ],
    "no-useless-constructor": "off",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "argsIgnorePattern": "_"
      }
    ],
    "@typescript-eslint/no-useless-constructor": "error",
    "camelcase": "off",
    "@typescript-eslint/camelcase": "off"
  },
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": "off"
      }
    }
  ],
  "settings": {
    "import/extensions": [".ts", ".js"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".js"]
    },
    "import/resolver": {
      "typescript": {
        "alwaysTryTypes": true
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

检测到依赖循环。eslintimport/no-cycle

关于(用户和部门)

和:

'type' 已定义但从未使用。允许未使用的参数必须匹配 /_/u.eslint@typescript-eslint/no-unused-vars

我无法解决这个问题,而且我不知道最好的选择是什么,我正在关注 typeorm 的入门

Nic*_*nov 9

循环导入的某些情况可能会让编译器感到困惑,但大多数时候它们可以毫无问题地解决。eslintimport/no-cycle规则的存在是为了尽早消除任何潜在的问题,这是一件好事。Stack Overflow上有一个很好的答案答案,它更深入地阐述了这个问题,并建议完全避免循环依赖。

\n

TypeORM 存储库中有一个讨论,提供了多种解决方案,每种解决方案都有自己的权衡。最完整的解决方案是实体模式字符串引用。两种解决方案都使用类似的想法 \xe2\x80\x93 来定义与字符串的关系并在稍后解析该字符串。

\n

这是解决循环依赖的常见方法,可以在其他 ORM 中找到,例如 Python 的 SQLAlchemy 或 Ruby on Rails 的 ActiveRecord。这个想法很简单:实体类/模式是通过实体的名称字符串从注册表中检索的,这消除了显式导入模块来定义关系的需要。

\n

让我举一个例子。

\n

实体模式

\n

这是定义实体的实用方法。类定义被替换为实例EntitySchema,并且不使用装饰器。

\n
export const DepartmentEntity = new EntitySchema({\n  name: "Department",\n  columns: { /* omitting for brevity */ },\n  relations: {\n    user: {\n      type: "one-to-one",\n      target: "User"\n    }\n  }\n})\n
Run Code Online (Sandbox Code Playgroud)\n
export const UserEntity = new EntitySchema({\n  name: "User",\n  columns: { /* omitting for brevity */ },\n  relations: {\n    department: {\n      type: "one-to-one",\n      target: "Department"\n    }\n  }\n})\n
Run Code Online (Sandbox Code Playgroud)\n

字符串引用

\n

这对您已经拥有的内容来说是一个不太激进的改变,它只需要将类型函数和反函数更改为字符串。要正确键入关系属性,您需要使用import type

\n
import type User from \'./user\';\n\n@Entity(\'Department\')\nexport default class Department {\n  // omitting most of the columns\n  // ...\n\n  @OneToOne(\'User\', \'department\')\n  @JoinColumn()\n  user: User;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
import type Department from \'./department\';\n\n@Entity(\'User\')\nexport default class User {\n  // omitting most of the columns\n  // ...\n\n  @OneToOne(\'Department\', \'user\')\n  department: Department;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

选择什么?

\n

由你决定。机制完全相同,但实体模式可能会保留下来,并成为未来 TypeORM 版本中的一等公民中的一等公民,并且它们为您提供比类提供的更多类型安全性。

\n