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

问题描述 投票:0回答:1

我正在为一个项目使用 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 nestjs eslint decorator typescript-decorator
1个回答
24
投票

Typescript 4.8.0 更改了 typescript-eslint 用来确定是否正在使用函数的底层 AST。 您应该能够将所有

@typescript-eslint/*
软件包更新到
@5.35.1
或更高版本
并且它应该都可以再次工作

© www.soinside.com 2019 - 2024. All rights reserved.