Javascript - (Static) Typing (Checker)

1 - About

Data Type - Static Typing in JavaScript.

2.1 - Checker

Typing can be added with this method:

  • TypeScript (by Microsoft). in Typescript, Type declaration files express the data types of parameters and functions. - - superset of Javascript that compiles plain Javascript
  • Flow Static Type Checker (by Facebook)
  • documentation annotation.

2.2 - Documentation Annotation

2.2.1 - 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"
  ]
}

2.2.2 - 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 


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap