Question

I am applying JSDoc annotations:

/**
 * @param {number} millis
 * @param {function} callback
 * */
function sleep(millis, callback) {
    setTimeout(function() {
        callback();
    }, millis);
}

Then what tool would tell there is an error for line below?

sleep('aaa', 'sdsdsd');

Is JSHint aware of JSDoc type annotations? How to enable such support in Eclipse?

Was it helpful?

Solution 2

Disclaimer, I'm the author of tern.java.

I suggest you that you install tern.java 1.0.0-SNAPSHOT. It provides the capability to validate your JavaScript files by using JSDoc annotations. Here a screenshot with the sample of this issue :

enter image description here

If you need improvement with JSDoc validation, please create issues here

OTHER TIPS

jshint is not aware of jsdoc comments. You could however look at using eslint and writing a plugin for it that would perform type checking on function calls. Depending on how your project is set up you might end up having to pre-parse your code to extract the comments into a rules file that your eslint rule would use.

You can take a look at https://github.com/yarax/typelint

It's an EsLint plugin that does type check for JSDoc comments, and in addition supports it's own extension to use Redux state or Swagger schemas (basically any JSON schema) for type check.

It helps not to write complex composite types from scratch and use existing data.

now VsCode can easily do that. there are 2 ways to activate vscode's typechecking based on jsDocs .

  1. comment // @ts-check at the top of the file you want it to be checked.
  2. to actiavete it for all files, create a file named jsconfig.json at the root of your project which contains the following: { "compilerOptions": { "checkJs":true } }

now enjoy type errors!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top