0

I cannot get to properly type the following piece of code:

/**                                                                                                                                                                                      
 * @constructor                                                                                                                                                                          
 */
function F() {
      this.a = 0;
};

/**                                                                                                                                                                                      
 * @type {function(number)}                                                                                                                                                              
 */
F.prototype.g = function(b) {
    this.a += b;
};

I get the following warning:

test.js:12: WARNING - could not determine the type of this expression
    this.a += b;
    ^

How can I properly type this in this example?

-- EDIT --

If you want to see the warning, you need to set reportUnknownTypes to trueas explained here. I am trying to get to 100% typed code and I figured I could not reach that for that simple a program.

Community
  • 1
  • 1
Mad Echet
  • 3,723
  • 7
  • 28
  • 44

2 Answers2

2
/** @type {function(number)} */ 

doesn't specify the "this" type so it is unknown. To specify it in that fashion you would want to use:

/** @type {function(this:F, number)}

Using "@param {number}" lets the compiler infer the "this" type from the fact it was declared on F's prototype.

John
  • 5,443
  • 15
  • 21
1

You seem to need to use @param {number} b instead of @type {function(number)}. Typing with @param doesn't throw the warning. Doesn't make a whole lot of sense, but it works:

/**                                                                                                                                                                                      
 * @param {number} b                                                                                                                                                             
 */
F.prototype.g = function(b) {
    this.a += b;
};

--

Original answer:

I think it's because you didn't type a in your constructor. Try this:

/**                                                                                                                                                                                      
 * @constructor                                                                                                                                                                          
 */
function F() {

    /**
     * @type {number}
     * @private
     */
    this.a = 0;
};
RoryKoehein
  • 3,113
  • 1
  • 14
  • 13
  • Thanks but it did not work either. I read that you did not need to type native types in variable definitions (this is why I skipped the `@type {number}`, as for `@private` it is not necessary I think. – Mad Echet Aug 25 '13 at 17:03
  • Thanks! Does not make a lot of sense I totally agree, I could never have found. – Mad Echet Aug 25 '13 at 23:13