Search code examples
typescriptnestjseslintdecoratortypescript-decorator

Eslint error `is defined but never used` warning in NestJs for all decorators


I'm using NestJs framework for a project. Today discovered that EsLint finds 587 wrong problems.

All decorators generate this error:

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
...

And all index files generate this errors

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'  

But all decorators are used and no index files has a named export. This is a sample of my DTO class

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;
...

And this is one of my index files:

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

And my 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`
        
      },
    },
  },
};

Solution

  • Typescript 4.8.0 changed the underlying AST that typescript-eslint was using to determine if functions were being used. You should be able to update all of your @typescript-eslint/* packages to @5.35.1 or higher and it should all be working again