Benoît
Benoît

Reputation: 15010

Why does not Closure type-check the parameters when using function.apply?

See below

/**
 * @param {string} a
 * @param {string} b
 */
var f = function(a, b){
    // ...
}

/**
 * @param {string} a
 * @param {boolean} c
 */
var h = function(a, c){
    f.apply(this, arguments); // no compile error
    f.apply(this, [a, c]);    // no compile error
    f.call(this, a, c);       // compile error: does not match formal parameter
}

Why does Closure raise an error only when using call and not apply?
Is there a way I can made closure type-check the parameters even when I'm using apply?

Upvotes: 6

Views: 174

Answers (1)

John
John

Reputation: 5468

Because (a) the type checker doesn't yet have the concept of an tuple type and (b) it is rare to call a method with an array literal. When using .call determining which argument is assigned to which parameter slot is trivial.

If the type system grows a tuple type, it would make sense to put more effort into checking .apply as the array "slot" types and length is more likely to be known.

Upvotes: 2

Related Questions