[英]How to annotate in javascript a class that extends a generic type, using closure compiler
I created a Class factory in javascript that takes type T and generates a new enhanced class of type T, simple. 我在javascript中创建了一个类工厂,它采用类型T并生成一个类型为T的新增强类,简单。
I'm using google's closure compiler to compile javascript, and closure's annotation to annotate Types etc. The challenge is how to annotate a Class that extends type T, where T is a generic type. 我正在使用谷歌的闭包编译器来编译javascript,并使用闭包的注释来注释类型等。挑战是如何注释扩展类型为T的类,其中T是泛型类型。
Here is a simplified Class Enhanced
that extends another type: 这是一个简化的Class Enhanced
,它扩展了另一种类型:
/**
* @constructor
*/
function Foo(){
this.x = 'x';
};
/**
* @constructor
*/
function Bar(){
this.y = 'y';
};
/**
* @constructor
* @param {function(new:T)} FooOrBar
* @extends {T}
* @template T
*/
function Enhanced(FooOrBar){
FooOrBar.call(this);
this.z = 'z';
};
/** @type Enhanced.<Foo> */
var enhancedFoo = new Enhanced(Foo);
/** @type Enhanced.<Bar> */
var enhancedBar = new Enhanced(Bar);
when I compile this code with the closure compiler I get a compiler warning: 当我用闭包编译器编译这段代码时,我得到一个编译器警告:
'Could not resolve type in @extends tag of Enhanced'. '无法解析增强型的@extends标签中的类型。
So, obviously the compiler does not know from where to infer T when this class is compiled. 因此,显然编译器在编译此类时不知道从哪里推断T.
Can someone please point out why the compiler can't infer the type when the class is created, or if there is a way to annotate a generated class as extending type T. 有人可以指出为什么编译器在创建类时无法推断出类型,或者是否有办法将生成的类注释为扩展类型T.
my use case is Class factories where the classes cannot be annotated because they're created at runtime 我的用例是类工厂,其中的类无法注释,因为它们是在运行时创建的
It sounds like the Closure Compiler may not be able to help you out here, at least not with this particular approach. 听起来Closure Compiler可能无法帮助你,至少不是这种特殊的方法。
You intend Enhanced
to be a function that takes a class and returns a class. 您希望Enhanced
是一个接受类并返回类的函数。 There are certainly languages in which operate on classes in this way (Python, Javascript, Ruby, etc.). 当然有一些语言以这种方式对类进行操作(Python,Javascript,Ruby等)。
But the Closure Compiler requires static classes in order to do its static analysis. 但Closure Compiler需要静态类才能进行静态分析。 That isn't necessarily the way that it has to work, but that is the way it was designed to work. 这不一定是它必须工作的方式,但这是它的工作方式。
A rough rule of thumb is this: if you can represent the types in Java, you can represent the types in Closure annotations. 一个粗略的经验法则是: 如果您可以用Java表示类型,则可以在Closure注释中表示类型。
Of course, this isn't GWT; 当然,这不是GWT; you're not writing Java. 你不是在写Java。 You can use your JS-fu to write all the meta classes you want. 您可以使用JS-fu编写所需的所有元类。 But don't expect the Closure Compiler to necessarily be able to type check it. 但是不要指望Closure Compiler必须能够键入检查它。
The good news is that there is literally decades of research and best practices behind languages following this paradigm of static classes: C++, Java, C#. 好消息是,在这种静态类范例之后,语言背后有数十年的研究和最佳实践:C ++,Java,C#。 If you are familiar with these, you might ask yourself "How would I do this in those languages?" 如果您熟悉这些,您可能会问自己“我将如何使用这些语言?”
Your actual problem (whatever it is) has likely been solved hundreds or thousands of times by a design pattern for these OO languages, and you could do the same. 您的实际问题(无论是什么)可能已经通过这些OO语言的设计模式解决了数百次或数千次,您也可以这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.