简体   繁体   English

使用Typescript从另一个应用程序模块中请求应用程序模块

[英]Requiring app module from within another app module with Typescript

I'm building a NodeJS application with Typescript, using typescript-require to compile my source code. 我正在使用Typescript构建一个NodeJS应用程序,使用typescript -require编译我的源代码。 One of my modules exports an interface that I'd like to implement in another of my modules. 我的一个模块导出了一个我想在另一个模块中实现的接口。

Given the following app structure, it does not appear possible to have one app-level module require another: 给定以下应用程序结构,似乎不可能有一个应用程序级模块需要另一个模块:

| app/
| -- node_modules/
| ---- interface/
| ------ index.ts
| ---- implements/
| ------ index.ts

Trying to import interface from inside implements .. 尝试导入interface从内部implements ..

import Interface = require('interface');

.. results in the following error: ..导致以下错误:

error TS2307: Cannot find external module 'interface'.

This works if implements is at the app root level. 如果implements位于应用程序的根目录级别,则此方法有效。

I can import the module just fine if I use var instead of import , but then I cannot implement the interface properly. 如果使用var而不是import ,则可以很好地导入模块,但是我不能正确实现该接口。

Is there any way around this? 有没有办法解决? Is this a limitation of typescript-require that I'm stuck with? 这是我坚持的打字稿要求的限制吗? Is there a better way to be able to write my Node modules in Typescript and be able to compile them on the fly? 有没有更好的方法可以用Typescript编写Node模块并可以即时对其进行编译?

Additional Information 附加信息

  1. I've tried using app-module-path , but that package explicitly prevents modules from requiring other modules in the node_modules folder. 我尝试使用app-module-path ,但是该包明确阻止模块在node_modules文件夹中使用其他模块。 Judging by the error above, I'm doubting compilation is even getting that far. 从上面的错误来看,我怀疑编译是否能走得那么远。

there a better way to be able to write my Node modules in Typescript 有一种更好的方法可以用Typescript编写Node模块

You need to use full relative path . 您需要使用完整的相对路径 There is discussion to remove this limitation : https://github.com/Microsoft/TypeScript/issues/2338 有讨论以消除此限制: https : //github.com/Microsoft/TypeScript/issues/2338

and be able to compile them on the fly 并能够即时编译它们

I wrote an IDE plugin that makes a:) relative paths easier ( https://github.com/TypeStrong/atom-typescript/#relative-paths ) and b.) does CompileOnSave ( https://github.com/TypeStrong/atom-typescript/#compile-on-save ) 我编写了一个IDE插件,该插件使a :)相对路径更容易( https://github.com/TypeStrong/atom-typescript/#relative-paths )和b。)做CompileOnSave( https://github.com/TypeStrong/原子类型脚本/保存时编译#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM