Google Closure Compiler 数组的类型注释

发布于 2024-12-11 15:24:29 字数 1262 浏览 0 评论 0原文

在使用 Google Closure Compiler 时,我发现了一种情况,我无法强制编译器对错误的变量类型发出警告。我使用了以下示例:

/** @typedef ({name: string, token: string}) */
var pendingItem;

/** @type (Array.<pendingItem>) */
var pending = [];

// NOTICE: the token is intentionally misspelled as "toke" to cause a warning
var dummyItem = {
  name: 'NameHere',
  toke: 'SomeToken' 
};

// This should cause a warning, because of the 
// type mismatch (toke instead of token)
pending.push(dummyItem);

// Do something useful so that the whole code wouldn't be optimized to 0 bytes
alert(pending.length);

它可以完美编译,而不是给出预期的类型警告。但如果您使用:

pending[1] = {
  name: 'Second Name',
  toke: 'Second Token'
}

您会收到预期的类型不匹配警告。

我知道这可能是因为 push 没有定义类型检查,因为它是一个内置函数。预计将 pending 定义为 pendingItem 数组会强制执行此操作,但默认情况下不会。

问题是是否以及如何向已定义的函数(例如 push)添加类型检查,以便在上面的示例中发出警告。我还了解到,实现类似结果的一种方法是在 dummyItem 之前添加 /** @type {pendingItem} */ 以强制类型,但出于教育目的,我想知道如何向 push 等函数添加类型检查(或者可能对 pending 本身进行更严格的定义)。

还有人可以解释重命名对象属性背后的逻辑是什么吗?如果编译上面的示例,它不会重命名 dummyItem 的属性 name,但会将 token 重命名为 a< /代码>。

While playing around with Google Closure Compiler, I found a case where I couldn't force the compiler to give a warning for a wrong variable type. I used the following sample:

/** @typedef ({name: string, token: string}) */
var pendingItem;

/** @type (Array.<pendingItem>) */
var pending = [];

// NOTICE: the token is intentionally misspelled as "toke" to cause a warning
var dummyItem = {
  name: 'NameHere',
  toke: 'SomeToken' 
};

// This should cause a warning, because of the 
// type mismatch (toke instead of token)
pending.push(dummyItem);

// Do something useful so that the whole code wouldn't be optimized to 0 bytes
alert(pending.length);

And it compiles perfectly, instead of giving the expected type warning. But if you use:

pending[1] = {
  name: 'Second Name',
  toke: 'Second Token'
}

You get the expected type mismatch warning.

I understand that it's probably because push doesn't have type-checking defined, since it's a built-in function. It would be expected that defining pending as an array of pendingItem would force it, but it doesn't by default.

The question is whether and how is it possible to add type checking to already defined functions such as push, so that it would give a warning in the example above. I also understand that one way to achieve a similar result would be to add /** @type {pendingItem} */ before dummyItem to force the type, but for educational purposes, I would like to know about adding type checking to functions like push (or maybe stricter definition of pending itself).

Also could somebody explain what is the logic behind renaming object properties? If you compile the above example, it wouldn't rename dummyItem's property name, but it would rename token to a.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-12-18 15:24:29

要强制对 push 进行类型检查,必须在代码中使用正确的类型重新定义它。请注意,使用对象文字表示法,您必须使用 @type 而不是 @param 来定义类型,因为我们将函数分配给对象的参数,而不是定义功能正常。在这种情况下,它将如下所示:

/** @type {function(pendingItem)} */
pending.push = function(item) {
  Array.prototype.push.call(pending, item);
};

现在,当尝试编译以下内容时:

// Still an intentionally misspelled "toke"
var dummyItem = {
  name: 'NameHere',
  toke: 'SomeToken' 
};

// First warning
pending.push(dummyItem);

// Second warning
pending[1] = {
  name: 'Second name',
  toke: 'Second Token'
};

然后,两者都会生成类型不匹配警告,如预期的那样。

认为它可能对未来的某人有用。

To force type checking on push, it has to be redefined in code with the correct type. Note that using object literal notation, you have to define the types by using @type instead of @param, because we are assigning the function to a object's parameter, not defining a normal function. In this case it would be the following:

/** @type {function(pendingItem)} */
pending.push = function(item) {
  Array.prototype.push.call(pending, item);
};

Now when trying to compile the following:

// Still an intentionally misspelled "toke"
var dummyItem = {
  name: 'NameHere',
  toke: 'SomeToken' 
};

// First warning
pending.push(dummyItem);

// Second warning
pending[1] = {
  name: 'Second name',
  toke: 'Second Token'
};

Then both will generate a type mismatch warning, like expected.

Thought that it might be useful to somebody in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文