javascriptgoogle-closurejsdoc

What does google closure's typedef actually do?


I understand what it is for - I can see the benefits of being able to define an alias for a complex type and use that in the documentation. So you can define a type something like...

/** @typedef {{x:number, y:number}} */
example.Point;

...and then use it to document a function, something like...

/**
 * @param {example.Point} point
 * @return {example.Point}
 */
example.functionThatTakesAPointAndReturnsAPoint(point) {
....
}

But the thing I'm not sure about is, if it's only used for documentation and the compiler's static type checking, then why does the typedef need that line of JavaScript? Couldn't the alias just be defined entirely within the documentation comment block? And, if you served the code directly (without compiling it), what would the JavaScript interpreter do with that line of code after the typedef comment?


Solution

  • The compiler is built on top of Rhino and just enhances the available syntax. I assume it was easier to take the alias from a no-op property access because it matches the standard pattern.