如何在多个项目中使用typescript文件,有些使用模块(node.js),有些则不使用?

时间:2022-01-29 14:40:47

I used typescript in the past mainly for client side code to run in the browser. Now I am trying to reuse some classes of my library for node.js. My setup looks like this:

我过去使用的typescript主要用于在浏览器中运行客户端代码。现在我正在尝试将我的库的一些类重用于node.js.我的设置如下:

  • SharedLibrary Project with multiple classes
  • 具有多个类的SharedLibrary项目

  • Client Project using the SharedLibrary
  • 使用SharedLibrary的客户端项目

  • Server Project with node.js using the SharedLibrary
  • 使用SharedLibrary的node.js的服务器项目

Node.js seems to force me to use commonjs as module library for typescript. Thus my code on the server side will be forced to use modules as well. How can I include my SharedLibrary now in this project? It seems modules are unable to access anything outside of the module except if it is itself a module but I also can't change my SharedLibrary to a module as this would force me to change my whole code base. Is there any way out of this without having to change everything?

Node.js似乎迫使我使用commonjs作为typescript的模块库。因此,我在服务器端的代码也将*使用模块。如何在此项目中包含我的SharedLibrary?似乎模块无法访问模块外的任何东西,除非它本身就是一个模块,但我也无法将我的SharedLibrary更改为模块,因为这会迫使我改变我的整个代码库。如果没有改变一切,有没有办法解决这个问题?

Example code: Library file A.ts:

示例代码:库文件A.ts:

class A {
    public call() {
        console.log("class A");
    }
}

Server file C.ts:

服务器文件C.ts:

export class C {
    public call(): void {
        console.log("class c");

        let a = new A(); //this will compile but crash during runtime
        a.call();

    }
}

node.js main file:

node.js主文件:

import * as myodule from "./C";
var s = new myodule.C();
s.call();

It will print "class c" and then crash as it can't find class A. It works just fine if I add export to "class A" and then import it but then my client side code stops working.

它将打印“class c”然后崩溃,因为它找不到A类。如果我将导出添加到“A类”然后导入它然后我的客户端代码停止工作它就可以了。

What I tried so far:

到目前为止我尝试了什么:

  • using the files directly doesn't work
  • 直接使用文件不起作用

  • using the export keyword to export class A outside of its .ts file doesn't seem to work either
  • 使用export关键字在其.ts文件之外导出类A似乎也不起作用

  • C style defines might work that optionally define the shared library as export or not should work but I couldn't find anything like that in typescript
  • C样式定义可能工作,可选择定义共享库,因为导出或不应该工作,但我在typescript中找不到类似的东西

1 个解决方案

#1


0  

Is there any way out of this without having to change everything?

如果没有改变一切,有没有办法解决这个问题?

Just use commonjs everywhere and use a module loader/bundler like webpack. Here is a quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

只需在任何地方使用commonjs并使用像webpack这样的模块加载器/捆绑器。这是一个快速入门:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

Example

Here is a fairly large project that uses this method http://alm.tools/

这是一个使用这种方法的相当大的项目http://alm.tools/

#1


0  

Is there any way out of this without having to change everything?

如果没有改变一切,有没有办法解决这个问题?

Just use commonjs everywhere and use a module loader/bundler like webpack. Here is a quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

只需在任何地方使用commonjs并使用像webpack这样的模块加载器/捆绑器。这是一个快速入门:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

Example

Here is a fairly large project that uses this method http://alm.tools/

这是一个使用这种方法的相当大的项目http://alm.tools/