【发布时间】:2016-05-24 04:16:07
【问题描述】:
我一直在努力用不同的方式在 Typescript 中制作模块,试图优雅地封装我的代码。但是,我想我对某些事情应该如何工作感到迷茫。
基本上,我正在尝试让两个项目一起工作:
- API 操作的类型定义 (https://github.com/Protectator/riotgamesapi-typedef)
- Node.js 中的一个库,使用这些定义进行调用 (https://github.com/Protectator/riotgamesapi-typenode)
类型定义似乎没问题。问题来自图书馆。因此,我试图将定义保留在名为 riotGamesApi 的模块中,并为库保留另一个名为 riotGamesTypeNode 的模块。我尝试在第二个中导入第一个,并且预编译器没有检测到任何错误。
我还开始为我的库使用 mocha 编写测试。同样,预编译器在我的代码中没有发现任何错误。
这是我目前对模块所做的:
文件riotgamesapi.d.ts(类型定义)
declare module riotGamesApi {
export module champion {
[...]
}
[some more exports]
}
declare module "riotGamesApi" {
export = riotGamesApi;
}
///<reference path="../lib/riotgamesapi-typedef/riotgamesapi" />
///<reference path="../typings/node/node" />
import * as api from "riotGamesApi";
export module riotGamesTypeNode {
[some classes here]
}
export = riotGamesTypeNode;
文件 riotgamesapi-typenode-tests.ts(库测试)
///<reference path="../lib/riotgamesapi-typedef/riotgamesapi" />
///<reference path="../src/riotgamesapi-typenode" />
import * as api from 'riotGamesApi';
import * as rtnode from '../src/riotgamesapi-typenode';
[Using rtnode in code]
但是在编译完所有东西后(使用commonjs 和es5 进行gulp),当我尝试运行测试时,遇到了这个错误:
events.js:141
throw er; // Unhandled 'error' event
^
Error: Cannot find module '../src/riotgamesapi-typenode'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:289:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
[...more error lines...]
我不明白我在这里做错了什么,因为这似乎是预编译器不会警告我某些事情的唯一方法。我肯定遗漏了一些东西,但预编译器也是如此?
我已链接到完整文件的内容,因此您可以在需要时查找更多信息,或者通过克隆 git 自己尝试。 (我为此使用gulp,测试代码的任务是gulp test)
提前感谢您提供的任何建议或解决方案,因为我已经被这个问题困扰了一段时间...... :)
【问题讨论】:
标签: module typescript require ecmascript-5 commonjs