Search code examples
typescripteslinttypescript-eslint

Disable eslint (typescript context) for a line when running eslint not working


I am struggling to disable a rule for one line of code.

I have this line of code:

public limit: number = 500

And my eslint --fix removes the type as it can be inferred. The problem is that class-validators requires that type so it knows what type to convert the string that is passed in.

I thus want to disable this "fix" for that line.

Right now I have:

  /* eslint-disable  @typescript-eslint/no-inferrable-types */
  /* eslint-disable   */
  public limit: number = 500 // eslint-disable-line @typescript-eslint/no-inferrable-types
  /* eslint-enable  @typescript-eslint/no-inferrable-types */

And when the estlint --fix runs it still removes the type.

Apparently nestjs and class-validator should work without the type - that might only apply to the latest version of nestjs which I am not on.

The bottom line however is that eslint disable rules does not work for me.


Solution

  • So the responses I got egged me on to try harder and this is what I now know, perhaps it will help someone.

    Consider that my field I'm wanting to change eslint rules for has decorators (and I assumed these would be irrelevant).

    This works:

      /* eslint-disable */
      @IsInt({ message: 'Limit is provided but it is not a valid integer' })
      @IsOptional()
      // number type is _required_ for implicit conversion to work
      public limit: number = 500
      /* eslint-enable */
    

    This, does not work... (notice how the disable comment is moved)

      @IsInt({ message: 'Limit is provided but it is not a valid integer' })
      @IsOptional()
      /* eslint-disable */
      // number type is _required_ for implicit conversion to work
      public limit: number = 500
      /* eslint-enable */
    

    So the decorators do make a difference. It is significant where the disable rule appears.