Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Closure's @typedef tag

Google's Closure compiler has an "@typedef" tag, but is it OK to use them in your code? (I know it'll work, but is it frowned upon?)

So here's my type

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

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

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

So the declared object isn't 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's parent's instructions
 * @return  {Instructions}              Returns the new instructions object
 */
Instructions = function(plan, manager, parent){
    plan.
}

So is this ok? Or is there a better solution?

like image 625
skerit Avatar asked May 10 '11 11:05

skerit


2 Answers

This is fine. You can also use a 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

like image 150
John Avatar answered Sep 22 '22 21:09

John


@typedef is used to define a type, not to mark an object as a certain type. If you want to mark a certain variable as a certain type, use the @type {<type>} annotation.

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

Beware that properties of objects are currently not typed in the Closure Compiler, even if marked, but may be in the future.

like image 44
Stephen Chung Avatar answered Sep 21 '22 21:09

Stephen Chung