继承带有导出的类和模块的TypeScript [英] Inheritance TypeScript with exported class and modules
问题描述
我对使用打字稿的继承感到疯狂。我不知道为什么,但它无法解析我想继承的类。
I'm getting crazy with inheritance using typescript. I don't know why but it cannot resolve the class I want to inherit.
lib / classes / Message.class。 ts
lib/classes/Message.class.ts
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
export module SharedCommunication {
export class Message{
// Stuff
}
}
lib / classes / ValidatorMessage.class。 ts
lib/classes/ValidatorMessage.class.ts
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
///<reference path='Message.class.ts'/>
export module SharedCommunication {
export class ValidatorMessage extends Message{
private _errors;
}
}
无法解析消息。我也尝试过SharedCommunication.Message,但它是一样的。我引用了课程,所以我根本不明白发生了什么。你有什么想法吗?
Message cannot be resolved. I tried SharedCommunication.Message too but it's the same. I reference the class so I don't understand at all what's going on. Do you have any idea?
我试过没有模块(两个类没有任何模块),但它是一样的。我需要导出类(以及模块,如果我使用它)从另一个node_module:typescript.api获取它们,我用它来加载类并在节点中使用它。
I tried without the module (two class without be in any module) but it's the same. I need to export the class (and the module if I use it) to get them from another node_module: typescript.api, which I use to load the class and use it in node.
lib / message.js
var Message = require('./classes/Message.class.ts');
module.exports = Message.SharedCommunication.Message;
这里的诀窍是什么?因为我在使用继承的不同文件夹中的同一项目上有源代码,没有模块或导出。谢谢。
What's the trick here? Because I have source code on the same project in a different folder working with inheritance, without module or export. Thanks.
推荐答案
ValidatorMessage.class.ts应如下所示:
ValidatorMessage.class.ts should look like this:
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
import message = require('./Message.class');
export module SharedCommunication {
export class ValidatorMessage extends message.SharedCommunication.Message {
private _errors;
}
}
单个<$ c $通常是多余的c>导出模块位于文件的顶层,因为文件本身无论如何都构成了一个命名空间。
It's usually redundant to have a single export module
at the top level of a file since the file itself constitutes a namespace anyway.
Bill在你的另一个问题中提到了这一点,但是如果你刚开始使用TypeScript,我会再次提醒使用RequireTS - 这听起来很不成熟,很可能引起很多混乱。
Bill mentioned this in your other question, but I'd again caution on using RequireTS if you're just starting out with TypeScript - it sounds pretty unmature and is likely to introduce a lot of confusion.
这篇关于继承带有导出的类和模块的TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!