Use so-called type-casting:
/** @type {Node} */ (e.target)
e.g.
goog.dom.findCommonAncestor(/** @type {Node} */ (e.target), img_wrapper[0])
Frage
I'm trying to compile my program which has a click event listener on an image and I am trying to check to see if that click happened on a particular element or on some other element.
function(e){
var img_wrapper = goog.dom.getElementsByClass('imgWrapper');
if (img_wrapper.length > 0 && goog.dom.findCommonAncestor(e.target, img_wrapper[0]) === img_wrapper[0]){
return;
}
}
When I compile, I get this error:
flickr_closure.js:226: WARNING - actual parameter 1 of goog.dom.findCommonAncestor does not match formal parameter
found : (EventTarget|null)
required: (Node|null|undefined)
if (img_wrapper.length > 0 && goog.dom.findCommonAncestor(e.target, img_wrapper[0]) === img_wrapper[0]){
^
0 error(s), 1 warning(s), 97.0% typed
The code works like I want it to, but I still get that warning, and since this is for a school assignment, one of the requirements is that I can't have warnings when compiled.
I've tried adding some basic type information for e.target
to try to tell the compiler to treat it as a Node, but it complains about that too.
Lösung
Use so-called type-casting:
/** @type {Node} */ (e.target)
e.g.
goog.dom.findCommonAncestor(/** @type {Node} */ (e.target), img_wrapper[0])
Andere Tipps
Expanding on Alex's answer, it is good practise to type-cast after actually checking the variable.
if(goog.dom.isElement(e.target)) {
.... /** @type {!Element} */ (e.target) ....
}