gpt4 book ai didi

javascript - 使用 Closure Compiler,Array 注释与仅 Array 相比有什么好处?

转载 作者:行者123 更新时间:2023-11-30 00:20:17 25 4
gpt4 key购买 nike

我希望 Closure Compiler 对下面的所有行发出警告,但只有 3) 显示为问题。 Type Checking Array Contents with Closure-Compiler触及同一点,但我的问题是,考虑到这些限制,注释 Array<T> 有什么好处?对比Array ?我觉得这是一种虚假的安全感。

// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name default.js
// @warning_level VERBOSE
// ==/ClosureCompiler==

/** @type {!Array<!string>} */
var xs = [];
xs.push(42); // 1) no warning
xs.push(null); // 2) no warning
xs = 'foo' // 3) warns - found: string, required: Array<string>
xs = [1,2,3]; // 4) no warning

快速编辑:您可以在 http://closure-compiler.appspot.com/ 试用此代码

最佳答案

我怀疑这是因为 externs definition for Array.prototype.push不太正确。如果您删除 @this 类型,那么您应该得到预期的错误。大概 {length: number} 部分是出于某种原因添加的,因此删除它可能会破坏某处的一些现有代码。

关于javascript - 使用 Closure Compiler,Array<T> 注释与仅 Array 相比有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com