NestJs 中所有装饰器的 Eslint 错误“已定义但从未使用”警告

2024-01-02

我正在为一个项目使用 NestJs 框架。今天发现EsLint发现了587个错误的问题。

所有装饰器都会生成此错误:

warning  'IsBoolean' is defined but never used      @typescript-eslint/no-unused-vars
warning  'IsEmail' is defined but never used        @typescript-eslint/no-unused-vars
warning  'IsEnum' is defined but never used         @typescript-eslint/no-unused-vars
warning  'IsInt' is defined but never used          @typescript-eslint/no-unused-vars
warning  'IsOptional' is defined but never used     @typescript-eslint/no-unused-vars
...

所有索引文件都会生成此错误

error  No named exports found in module './users.controller'  import/export
error  No named exports found in module './users.service'     import/export
error  No named exports found in module './users.module'  

但是使用了所有装饰器,并且没有索引文件具有命名导出。 这是我的 DTO 类的示例

import {
  IsBoolean,
  IsEmail,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  Matches,
  Max,
  MaxLength,
  Min,
  MinLength,
} from 'class-validator';

export class UpdateUserDto {
  @IsOptional()
  @IsString()
  @Matches(/^[a-zA-Z0-9-_.@]+$/)
  @MinLength(3)
  @MaxLength(32)
  @ToCase({strategy: 'lower'})
  @Trim()
  username?: string;
...

这是我的索引文件之一:

export * from './users.controller';
export * from './users.service';
export * from './users.module';
export * from './auth.controller';
export * from './auth.service';

还有我的 eslintrc.js:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint/eslint-plugin', 'import'],
  extends: [
    './node_modules/gts/',
    'plugin:@typescript-eslint/recommended',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
    'plugin:import/errors',
    'plugin:import/warnings',
    'plugin:import/typescript',
  ],
  root: true,
  env: {
    node: true,
    jest: true,
  },
  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': 'warn',
    'import/no-unresolved': 'error',
    'import/no-cycle': 'warn',
    'node/no-extraneous-import': [
      'error',
      {
        allowModules: ['express'],
      },
    ],
  },
  settings: {
    ['import/parsers']: {'@typescript-eslint/parser': ['.ts', '.tsx']},
    ['import/resolver']: {
      node: {
        extensions: ['.ts'],
        moduleDirectory: ['node_modules', 'src/'],
      },
      typescript: {
        alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
        
      },
    },
  },
};

Typescript 4.8.0 更改了 typescript-eslint 用于确定是否正在使用函数的底层 AST。您应该能够更新您的所有@typescript-eslint/*包裹到@5.35.1或更高 https://github.com/typescript-eslint/typescript-eslint/issues/5525#issuecomment-1226256085一切都应该恢复正常了

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NestJs 中所有装饰器的 Eslint 错误“已定义但从未使用”警告 的相关文章

随机推荐