[英]Declaration file for class-module (TypeScript)
I use a node module that's not found by typings and doesn't exist in definelytyped. 我使用的节点模块无法通过键入找到,并且在definelytyped中不存在。
the basic use of the module is: 该模块的基本用途是:
import * as SomeClass from 'some-module';
var someObject = new SomeClass("some string");
someObject.someMethod();
As you can see, this module exports a class as its default. 如您所见,此模块导出一个类作为其默认值。 I haven't figured out how to write the declaration file for it. 我还没有弄清楚如何为它编写声明文件。
This is the best I managed to do: 这是我设法做到的最好:
declare module 'some-module' {
export default class SomeClass {
constructor (someArg: string);
someMethod(): void;
}
}
BTW it does work JavaScriptly. 顺便说一句,它确实可以使用JavaScript。 It's only the TypeScript bothers me. 只是TypeScript困扰我。
Any ideas how to solve that? 任何想法如何解决?
For the declaration, you need to do this: 对于声明,您需要执行以下操作:
declare module 'some-module' {
class SomeClass {
constructor (someArg: string);
someMethod(): void;
}
namespace SomeClass {}
export = SomeClass;
}
UPDATE: Thank you to Blake Embrey for pointing out, the hack namespace SomeClass {}
is needed to get this working. 更新:感谢布莱克·恩布雷(Blake Embrey)指出,需要使用hack namespace SomeClass {}
来使此工作正常。 see https://github.com/Microsoft/TypeScript/issues/5073 for more detail. 有关更多详细信息,请参阅https://github.com/Microsoft/TypeScript/issues/5073 。
如果您只是想避免TypeScript错误并且不担心ItelliSense,只需像在文件开头那样声明类库即可,例如:
declare var SomeClass: any;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.