Using the Google Closure Tag @typedef

The Google Closure compiler has an @typedef tag, but can I use them in my code? (I know this will work, but frowned?)

So here is my type

  / **
  * The plan object typedef
  * @typedef {Object}
  * /
 Types.Plan = {
     "style": "bordersmall",
     "width": "50%",
     "height": "40%",
     "x": "20%",
     "y": "10%",
     "clickable": true,
     "moveable": true
 };

And then I can use this type in my JSDoc annotations.

This allows my IDE to give me autocomplete on the passed parameter

Thus, the declared object is not used anywhere in the code.

  / **
  * The Instructions class
  * @param {Types.Plan} plan Plan for position and dimension
  * @param {Manager} manager The manager
  * @param {Instructions} parent This widget parent instructions
  * @return {Instructions} Returns the new instructions object
  * /
 Instructions = function (plan, manager, parent) {
     plan.
 }

So is that normal? Or is there a better solution?

+6
source share
2 answers

@typedef used to determine the type, and not to designate an object as a specific type. If you want to mark a specific variable as a specific type, use the @type {<type>} annotation.

@typedef used to define "short" types for use with @type {...} constructs.

Beware that object properties are not currently printed in the Closure compiler, even if they are checked, but may be in the future.

+7
source

This is normal. You can also use the record type to enable additional type checking with the compiler:

 /** * @typedef {{ * style: string, * width: string, * height: string, * x: string, * y: string, * clickable: boolean, * moveable: boolean * }} */ var myType = ... 

http://code.google.com/closure/compiler/docs/js-for-compiler.html#types

+6
source

Source: https://habr.com/ru/post/887714/


All Articles