[英]Exporting hybrid type and module
I've been happily developing for months in TypeScript using classes with "attached" modules in CommonJS external modules as follows : 我几个月来一直在TypeScript中使用在CommonJS外部模块中具有“附加”模块的类来愉快地进行开发,如下所示:
exports = JQ;
class JQ {
a = 0;
}
module JQ {
export class HelpClass {}
export interface Something {}
}
I like this pattern because it makes modules contained in files, reduce the number of imports, and still exposes a nice API. 我喜欢这种模式,因为它使模块包含在文件中,减少了导入次数,并且仍然公开了一个不错的API。
Now, instead of exporting a class I want to export a Hybrid Type, so that it can be used as a function having other functions and classes "inside", like for example jQuery, Underscore, and many other libraries does. 现在,我不想导出类,而是要导出混合类型,以便可以将其用作具有“内部”其他函数和类的函数,例如jQuery,Underscore和许多其他库。
I know how to declare and how to create a hybrid type in TypeScript, however don't manage to export it with an attached module. 我知道如何在TypeScript中声明以及如何创建混合类型,但是无法通过附加模块导出它。 I do obtain the right emitted JS code, but the compiler complains.
我确实获得了正确发出的JS代码,但是编译器抱怨。
For example, this : 例如:
export = JQ;
var JQ = <JQ.JQStatic>function (a:string) { return a; };
JQ.method = function() { return 1; };
module JQ {
export interface JQStatic {
(a:string):string;
method() :number;
}
// Additional class here
export class SomethingElse {}
}
Produces what looks like the right JS code (looks exactly like the one generated if JQ was a class and not a hybrid type) but the compiler gives duplicate identifier error, and intellisense stop working. 产生看起来正确的JS代码(看起来与如果JQ是类而不是混合类型时生成的代码完全一样),但是编译器给出重复的标识符错误,并且intellisense停止工作。
If I remove the additional class SomethingElse
, it works correctly, so I suppose that's a special case to export an hybrid type. 如果我删除附加类
SomethingElse
,则它可以正常工作,因此我认为导出混合类型是一种特殊情况。
I tested it also on TypeScript Playground and got same results: despite class JQ
and var JQ
both emit a var JQ
in JS, when it's a class TypeScript allows and accepts it, otherwise not. 我也在TypeScript Playground上对其进行了测试,并得到了相同的结果:尽管
class JQ
和var JQ
都在JS中发出了var JQ
,但当它是一个类时,TypeScript允许并接受它,否则就没有。
Is what I'm trying to do unsupported? 我尝试做的事情不受支持吗? However, since the emitted JS is valid, so it's a feature already in the compiler, should this be reported as a bug to TypeScript devs?
但是,由于发出的JS是有效的,所以它是编译器中已经存在的功能,是否应将它报告为TypeScript开发人员的错误?
function JQ(a: string) { return a; }
module JQ {
export class SomethingElse { }
export function method() { return 1; }
}
export = JQ;
See https://github.com/Microsoft/TypeScript/wiki/Declaration-Merging 参见https://github.com/Microsoft/TypeScript/wiki/Declaration-Merging
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.