【问题标题】:TypeScript: plugin:prettier/recommended for eslint, unable to use the override keywordTypeScript:plugin:prettier/recommended for eslint,无法使用 override 关键字
【发布时间】:2021-12-14 16:49:17
【问题描述】:

软件包版本:

eslint@7.32.0
prettier@2.4.1
eslint-plugin-prettier@4.0.0

我有以下 eslintrc,这只是 NestJS 的库存配置:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    sourceType: 'module',
  },
  plugins: [
    '@typescript-eslint/eslint-plugin',
    'filenames'
  ],
  extends: [
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
  ],
  root: true,
  env: {
    node: true,
    jest: true,
  },
  ignorePatterns: ['.eslintrc.js'],
  rules: {
    '@typescript-eslint/interface-name-prefix': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'filenames/match-regex': [2, "^[0-9a-z-.]+$", true]
  },
};

这是更漂亮的配置:

{
  "singleQuote": true,
  "trailingComma": "all"
}

这是 tsconfig:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "esModuleInterop": true,
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "rootDir": ".",
    "incremental": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "noImplicitOverride": true
  }
}

在我的代码中,我有一个这样的类:

@Injectable()
export class FirebaseStrategy extends PassportStrategy(
  FirebaseAuthStrategy,
  'firebase',
) {
  constructor(private usersService: UsersService) {
    super({ extractor: ExtractJwt.fromAuthHeaderAsBearerToken() });
  }

  override async validate(payload: FirebaseUser): Promise<User | undefined> {
    return this.usersService.findByFirebaseUid(payload.uid);
  }
}

这段代码运行良好,但是当我尝试 lint 时,出现以下错误:

/app/src/auth/strategy/firebase.strategy.ts
  22:12  error  Parsing error: Unexpected token  prettier/prettier

有没有办法解决这个问题?我不希望有隐式覆盖。

【问题讨论】:

标签: typescript eslint prettier


【解决方案1】:

看起来您需要将 prettier 更新到 2.3.1 之后的任何版本 - 该版本中添加了对 TypeScript 4.3 功能(如 override 关键字)的支持。你可以在 package.json 中更新它或者使用npm i -D prettier@^2.3.1(假设你使用 npm 作为你的包管理器)。

【讨论】:

  • 我的 pakcage.json 中有更漂亮的 "^2.3.2",猜不是这样
  • 对不起,除了尝试更新所有 pretter/eslint 软件包之外我不知道
猜你喜欢
  • 2022-06-16
  • 2020-11-20
  • 2019-07-26
  • 2022-10-23
  • 2015-05-22
相关资源
最近更新 更多