4

I'm using eslint on my angularjs project and I prefer using vanillajs the more I can.

With that being said, I don't get the reason of why eslint consideres the following expression as invalid and refuses it. Is using typeof considered a bad practice?

if (typeof value === 'string') { ... } // eslint error angular/typecheck-string

Ref: https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-string.md

A. Gille
  • 912
  • 6
  • 23
  • No, its `You should use the angular.isString method` – A. Gille Jun 14 '18 at 15:45
  • 1
    Because you are leveraging a framework. Let the framework do the work for you. – zero298 Jun 14 '18 at 15:47
  • how say @zero298 you have to use angular.isString(value); – Pasquale De Lucia Jun 14 '18 at 15:52
  • @zero298 I don't see any benefit to use this built-in function, which will be slower than `typeof`, while JS can handle this in a simple way. I understand that sometimes it is more convenient to use handy built-in functions instead of long-and-ugly JS – A. Gille Jun 14 '18 at 15:57

3 Answers3

3

It's because you're using Angular, which has a built-in isString method, and you have the Angular plugin installed for eslint. It's going to recommend using features that are already implemented for you if you can.

Caldwell
  • 43
  • 1
  • 3
  • Why does it raises an error and not just a warning then? I don't see any bad practice nor erronous usage of JS here – A. Gille Jun 14 '18 at 15:53
  • 1
    @A.Gille one person's warning is another person's error. Some would consider using tabs instead of spaces a war crime. It's perfectly valid to alter your eslint configuration for you own project according to your personal/organization's standards. – zero298 Jun 14 '18 at 15:58
  • @zero298 Ok. I thought that some concrete deep valid programming reasons were hidden behind this, but apparently it is just a way to keep code coherent – A. Gille Jun 14 '18 at 16:01
  • 1
    @A.Gille you could [ask the implementer yourself](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-string.md) – Caldwell Jun 14 '18 at 16:01
1

They probably prefer you to use that built-in because typeof checking is prone to typo errors.

If you accidentally mistyped "string" for example, you would get undesired behavior, and it wouldn't be immediately obvious what the problem is. That mistake isn't possible if you use an existing, established function to check.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1

To go along with Caldwell's answer, you can reconfigure your eslintrc file and override type checking linting.

See: typecheck-string.

There are also rules for the other typechecks that Angular has utility functions for.

I don't know what form of eslintrc file you are using, but for .eslintrc.js it would be:

module.exports = {
  rules: {
    "angular/typecheck-string": "warn" // or "off" if you want no feedback
  }
}
zero298
  • 25,467
  • 10
  • 75
  • 100