简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM