错误的类型注释.未知类型 [英] Bad type annotation. Unknown type

查看:85
本文介绍了错误的类型注释.未知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到上述警告,但不知道为什么.我添加了deps.js,并且deps.js包含对该类型的引用.

I keep getting the warning mentioned above but can't figure out why. I've added deps.js and deps.js contains a reference to the type.

这是令人讨厌的代码:

goog.provide("MyCompany.MyApp.dom");


MyCompany.MyApp.dom.getArticleAmountInput_=function(){
    return document.getElementById("articleAmount");
};
/**
 * Gets the article amount input value
 * @type {function(this:MyCompany.MyApp.dom):number} */
MyCompany.MyApp.dom.getArticleAmount=function(){
    var tmp=this.getArticleAmountInput_();
    return (tmp)?tmp.value():undefined;
};

在deps.js中:

goog.addDependency('../../MyCompany/MyApp/dom.js', [ 'MyCompany.MyApp.dom'], []);

代码在html中加载,因此它会在运行时找到该类.这是我编译代码的方式:

Code loads in html so it does find the class at runtime. Here is how I compile the code:

java -jar ../../compiler.jar \
--compilation_level=ADVANCED_OPTIMIZATIONS \
--formatting=PRETTY_PRINT \
--warning_level=VERBOSE \
--summary_detail_level=3 \
--js=MyCompany/MyApp/dom.js \
--js=closure/goog/deps.js \
--js_output_file=out.js

它不断向我发出警告:

警告-错误的类型注释.未知类型MyCompany.MyApp.dom

WARNING - Bad type annotation. Unknown type MyCompany.MyApp.dom

[更新]

试图在编译时完全忽略goog.provide并忽略js=closure/goog/deps.js,并将所有内容更改为小写,但在此代码上始终收到相同的警告:

Tried to leave out the goog.provide completely and leave out the js=closure/goog/deps.js when compiling and changed everything to lower case but keep getting the same warning on this code:

//goog.provide("mycompany.myapp.dom");
var mycompany={};
mycompany.myapp={};
mycompany.myapp.dom={};

mycompany.myapp.dom.getArticleAmountInput_=function(){
    return document.getElementById("articleAmount");
};
/**
 * Gets the article amount input value
 * @type {function(this:mycompany.myapp.dom):number} */
mycompany.myapp.dom.getArticleAmount=function(){
    var tmp=this.getArticleAmountInput_();
    return (tmp)?tmp.value():undefined;
};

[更新]

问题是,当我添加一个typedef时,我会得到警告myapp.dom is never defined,但是当我让代码确定类型是什么时,我会得到Bad type annotation.

The thing is that when I add a typedef I get warnings that myapp.dom is never defined but when I let the code determine what the type is I get Bad type annotation.

试图添加如下所示的typedef:

Tried to add a typedef like so:

/**
 * @typedef {{getArticleAmount:function(this:myapp.dom):?number}}
 */
myapp.dom;

但是请不要真正理解为什么goog.provide应该创建myapp.dom,而编译器应该知道这一点.

But don't realy see why as goog.provide should create the myapp.dom and the compiler should know that.

[更新]

我已经从构造函数创建了以下myapp.workflow.现在,编译器应该可以识别类型,并且Eclipse可以进行代码辅助.不知道这是否是这样做的方法,但我将尝试重构一个小项目.

I have created the following myapp.workflow from constructors. Now the compiler should recognize the type and Eclipse can code assist. Not sure if this is the way to do it but I'll try to re factor a small project.

在types.js中设置主要类型

Setting up the main types in types.js

// source: js/mmyapp/types.js
goog.provide("myapp.types");
/** @constructor */
var gooblediegoog=function(){};
/** @constructor */
gooblediegoog.prototype.WorkFlow=function(){};
/** @constructor */
gooblediegoog.prototype.Dom=function(){};
myapp.types=new gooblediegoog();

我的代码中根本没有使用过但告诉Eclipse如何自动完成的文件:

A file that isn't used at all in my code but tells Eclipse how to auto complete:

// source: js/myapp/forCodeAssist.js
/** @const {boolean} */
var ALLWAYSFALSE=false;

if(ALLWAYSFALSE){
    /**needed for Eclipse autocomplete
     * @constructor
     */
    var MyApp=function(){};
    MyApp.prototype.types=new gooblediegoog();
    Window.prototype.myapp=new MyApp();
    MyApp.prototype.workflow=new myapp.types.WorkFlow();
    MyApp.prototype.dom=new myapp.types.Dom();
}

工作流程的实现:

// source: js/myapp/workflow.js
goog.provide("myapp.workflow");
goog.require("myapp.types");
goog.require("myapp.dom");

/** @returns number|undefined */
myapp.types.WorkFlow.prototype.createOrder=function(){
    return myapp.dom.getArticleAmout();
};
myapp.workflow=new myapp.types.WorkFlow();
window['console'].log(myapp.workflow.createOrder());

通过将myapp.types.WorkFlow.prototype替换为myapp.workflow,删除myapp.workflow=new myapp.types.WorkFlow()并删除goog.require("myapp.types"),可以将其转换为myapp.workflow.createOrder=...语法.如果需要,也许可以在构建/编译过程中将其自动化.

This can be converted to a myapp.workflow.createOrder=... syntax by replacing myapp.types.WorkFlow.prototype with myapp.workflow, removing myapp.workflow=new myapp.types.WorkFlow() and removing the goog.require("myapp.types"). Maybe this can be automated in the build/compile process if needed.

我不确定在构造函数的帮助下创建单个对象是否比仅使用goog.require创建myapp.workflow并向其添加属性的方法要昂贵得多(就像在闭包中完成的那样)库).

I am not sure if creating a single object with the help from a constructor function is much more expensive than just having goog.require create myapp.workflow and adding properties to it as I used to do (and as it's done in closure library).

推荐答案

匿名类型(命名空间或单例)在Closure-compiler中没有特定类型,因此不能在注释中用作类型名称.只有构造函数和接口可以用作新的类型名称. (可以使用typedef,但实际上只是速记注释)

Anonymous types (namespaces or singletons) don't have specific types in Closure-compiler and cannot be used as a type name in an annotation. Only constructors and interfaces can be used as new type names. (typedefs can be used but are really just shorthand annotations)

在许多情况下,编译器将正确推断类型,而您无需对其进行注释.在您的特定情况下,问题似乎出在您使用this关键字.在静态对象/名称空间中使用this关键字是危险的,不被支持.编译器将发出警告.虽然可以使用@this注释禁止显示警告,但这不是正确的操作,因为您没有通过使用.call.apply来具体指定this引用.

In many cases the compiler will correctly infer the types and you will not need to annotate it. In your specific case, it looks like the problem is your use of the this keyword. Using the this keyword in a static object / namespace is dangerous and not supported. The compiler will raise a warning. While you can suppress the warning with a @this annotation, that is not the correct action here as you are not specifically specifying the this reference by using .call or .apply.

解决方案是使用完整的对象名称:

The solution is to use the full object name:

/**
 * Gets the article amount input value
 */
mycompany.myapp.dom.getArticleAmount=function(){
  var tmp=mycompany.myapp.dom.getArticleAmountInput_();
  return (tmp)?tmp.value():undefined;
};

有关完整的技术细节,请参考此是你的这个帖子.

For the full technical details, reference the This this is your this post.

这篇关于错误的类型注释.未知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆