@type annotation and type checking

44 views
Skip to first unread message

Thomas Allen

unread,
Aug 27, 2012, 10:02:28 AM8/27/12
to Closure Compiler Discuss
Hi, I think that I am using @type incorrectly. I define the following
constructor:

/**
* Container widget.
*
* @constructor
* @extends {goog.ui.Container}
* @param {?goog.ui.Container.Orientation=} opt_orientation Container
* orientation; defaults to VERTICAL.
* @param {?goog.ui.ContainerRenderer=} opt_renderer Renderer used to
* render or decorate the container; defaults to
goog.ui.ContainerRenderer.
* @param {?goog.dom.DomHelper=} opt_domHelper DOM helper, used for
* document interaction.
*/
myNs.Container = function(opt_orientation, opt_renderer,
opt_domHelper) {
goog.base(this, opt_orientation, opt_renderer, opt_domHelper);
this.setRenderer(/** @type {?goog.ui.ContainerRenderer} */
opt_renderer ||
goog.ui.registry.getDefaultRenderer(this.constructor));
};
goog.inherits(myNs.Container, goog.ui.Container);

And I will see the following warning with type checking enabled:

found : (goog.ui.ContainerRenderer|goog.ui.ControlRenderer|null)
required: (goog.ui.ContainerRenderer|null)
this.setRenderer(/** @type {?goog.ui.ContainerRenderer} */
opt_renderer ||

The trouble is that goog.ui.registry.getDefaultRenderer is typed
{goog.ui.ControlRenderer?} although it has no reason to be that type
(it works fine for goog.ui.ContainerRenderer). I don't mind that very
much, because I should be able to override with the @type annotation,
right? What do I do in this case? I don't typically enable type
checking so this is my first time encountering this issue.

Thanks,
Thomas Allen

Nick Santos

unread,
Aug 28, 2012, 12:51:37 AM8/28/12
to closure-comp...@googlegroups.com
you need a set of parens around what you're trying to cast.
/** @type {?goog.ui.ContainerRenderer} */ (
opt_renderer ||
goog.ui.registry.getDefaultRenderer(this.constructor)
)
Reply all
Reply to author
Forward
0 new messages