【发布时间】:2015-10-30 01:38:35
【问题描述】:
我希望 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/ 使用此代码
【问题讨论】:
-
Javascript 没有类型限制。您可以
var xs = true;然后xs++;除非其他解释器有限制,否则您将没有安全性....但是,如果您执行意外操作,大多数系统会通知您。据我所知,这是唯一的优势。
标签: javascript google-closure-compiler google-closure typechecking