场景如下:
使用 Swagger,我为我的 REST 服务创建了一个 API 规范。然后,我生成角度 typescript 样板文件,用于从 Swagger 编辑器工具调用我的服务。
当我生成样板文件时,有多个文件都使用 typescript 内部命名空间,而我项目的其余部分是 AMD,因此我试图将此编译视为第三方非 AMD 库。当我编译我发送到单个 javascript 文件的 typescript 文件时。
我的项目使用 RequireJS,我已经像这样用 RequireJS 注册了我的单个 javascript 文件:
requirejs.config({
paths: 'myApi': 'my/path/to/myApi', // There is no ts file with this name but there is a js once compiled
shim: {
'myApi': {
init: () => {
return this.API; // The top level object from the boilerplate is API
},
exports: 'myApi'
}
}
}
})
在另一个文件中我有代码:
import myApi = require('myApi');
console.log(myApi);
在我上面的同一个文件中:
import angular = require('angular');
它以与 myApi 类似的方式注册到 RequireJS。 Typescript 在查找角度时没有问题。
当我编译我的项目时,我得到了 typescript
error TS2307: Cannot find module 'myApi' from the file trying to import it.
当我运行实际项目时,正确的 myApi 对象被记录到控制台。
我怎样才能让 Typescript 看到这个模块(或者以其他方式抑制这个错误消息)?
最佳答案
我要采用的解决方案是生成我的文件,然后手动将它们修改为外部模块而不是内部模块。另一种方法是修改 swagger codegen 源以将我的文件生成为外部文件,但目前必须采用手动方式。
关于typescript - 错误 TS2307 : Cannot find module (but it can when running the js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34799510/