Javascript - (Static) Typing (Checker)

About

Data Type - Static Typing in JavaScript.

Checker

Typing can be added with this method:

Documentation Annotation

Jsdoc

JSDoc annotation

in js file: with Typescript checkJs (to true) and Jsdoc Ref

{
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true,
    "checkJs": true,
    "target": "es2017",
    "module": "commonjs"
  },
  "include": [
    "lib"
  ]
}

Closure

the Google Closure types syntax (works within Idea).

  • Simple Type: Type of parameter or variable is specified on the left side
/** @param {number} x **/
function inc(x) {return x + 1;}
  • Constructor Type: Every constructor function “becomes” a type and its name can be referenced inside JSDoc tags.
function Point(x,y) { this.x = x; this.y = y; }

/** @param {Array.<Point>} points 
* Array of elements of some type could also be specified with [] (for example, Point[]).
*/
function printPoints(points) {
...
}

  * It works also for the DOM
<code javascript>
// @param {Node} domNode
// @param {HTMLElement} htmlElement
  • Return Type
/** @return {!Shape|undefined} */
  • Type annotation
/** @type {function(string, *)} */
function 

Powered by ComboStrap