首页 > 解决方案 > TypeScript:plugin:prettier/recommended for eslint,无法使用 override 关键字

问题描述

软件包版本:

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

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

标签: typescripteslintprettier

解决方案


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


推荐阅读