能否更新Google Closure Compiler中AbortController的extern并消除参数警告?
解决Google Closure Compiler中AbortController.abort参数警告的方法
针对你遇到的Closure Compiler v20221102版本因内置extern定义未更新导致的AbortController.prototype.abort参数警告,有以下几种可行方案:
1. 自定义externs文件覆盖定义
创建一个自定义externs文件(比如custom_abort_externs.js),重新定义支持reason参数的abort方法:
/** * 终止信号,可传入终止原因 * @param {*=} reason 可选的终止原因参数 * @return {void} */ AbortController.prototype.abort = function(reason) {};
编译时将该文件加入externs列表,优先级高于内置externs,编译器会使用你定义的方法签名:
closure-compiler --externs path/to/custom_abort_externs.js ...[其他编译参数]
2. 局部类型转换忽略警告
如果不想新增externs文件,可以在调用abort的地方通过类型注释强制指定函数签名,绕过编译器检查:
setTimeout(() => { /** @type {function(*): void} */(controller.abort)(reason); }, wait);
这种方式适合临时快速解决问题,但代码侵入性较强。
3. 升级Closure Compiler版本
检查Closure Compiler后续版本的更新日志,较新的版本大概率已经更新了w3c_abort.js中的extern定义,适配了浏览器对reason参数的支持。直接升级到新版本即可消除警告。
内容的提问来源于stack exchange,提问作者Norm
相关产品推荐
相关产品推荐

