Google Closure Compiler Type Annotations For Array
While playing around with Google Closure Compiler, I found a case where I couldn't force the compiler to give a warning for a wrong variable type. I used the following sample: /**
Solution 1:
To force type checking on push
, it has to be redefined in code with the correct type. Note that using object literal notation, you have to define the types by using @type
instead of @param
, because we are assigning the function to a object's parameter, not defining a normal function. In this case it would be the following:
/** @type {function(pendingItem)} */
pending.push = function(item) {
Array.prototype.push.call(pending, item);
};
Now when trying to compile the following:
// Still an intentionally misspelled "toke"
var dummyItem = {
name: 'NameHere',
toke: 'SomeToken'
};
// First warning
pending.push(dummyItem);
// Second warning
pending[1] = {
name: 'Second name',
toke: 'Second Token'
};
Then both will generate a type mismatch warning, like expected.
Thought that it might be useful to somebody in the future.
Post a Comment for "Google Closure Compiler Type Annotations For Array"