4

Say I have this piece of code:

/** @type {string} */
const foo = '123';
const bar = foo.map((c) => c + 1);

Then I'd like ESLint to tell me I've got an error here: map is not a function of a string type

I use WebStorm as my IDE and it recognizes the issue, but I'd like to be able to recognize these issues using my linter from the command line.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
YardenST
  • 5,119
  • 2
  • 33
  • 54
  • 1
    This sounds like something you would use a language like Typescript for, and out of scope for a linter. – Flimm Nov 19 '18 at 15:09
  • 3
    @Flimm `A linter or lint refers to tools that analyze source code to flag programming errors, bugs, stylistic errors, and suspicious constructs` Wikipedia. I think it is in its scope? :) – YardenST Nov 19 '18 at 15:12
  • Google closure compiler more or less does just that. Facebook's flow analyzer should also catch that, even without the annotation. It is definitely out of scope for a linter, you just need a different tool. – Jared Smith Nov 19 '18 at 15:15

1 Answers1

3

ESLint does not do this, but one can check javascript files, not just typescript files, with Typescript: https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files

It supports quite a few JSDoc comments: https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript + it infers types when nothing has been explicitly defined.

Here's the Typescript config of an javascript project of mine: https://github.com/voxpelli/node-promised-retry/blob/67512edc4f414d128279f25268d860d9f10d2be0/tsconfig.json

VoxPelli
  • 2,697
  • 1
  • 16
  • 22