简体   繁体   English

如何将现有的打字稿命名空间类用作Node.js的外部模块

[英]How to use Existing typescript namespaced classes as external modules for nodejs

The simple question here is how do I use namespaced typescript classes as external modules for use in node. 这里一个简单的问题是如何使用命名空间的打字稿类作为外部模块供节点使用。 The answer to that seems to be 答案似乎是

It can't be done, don't try it, don't use namespaces 它无法完成,请勿尝试,请勿使用名称空间

Well I am in the position that we have a large codebase that is all written in namespaces and we are now trying to use this code in node.js as well. 好吧,我的位置是,我们有一个大型的代码库,所有代码库都是用命名空间编写的,我们现在也尝试在node.js中使用此代码。

I am trying to figure out how to use our existing code. 我试图弄清楚如何使用我们现有的代码。

I have got as far as compiling my namespaced code into a commonjs nodelibs folder that produces one js file and one d.ts for each ts file. 我已经将我的命名空间代码编译到一个commonjs nodelibs文件夹中,该文件夹生成一个js文件和每个ts文件一个d.ts

I have to add the following to each of my ts files that I want to be available to node: 我必须将以下内容添加到我想对节点可用的每个ts文件中:

namespace Organisation.Project.Entities{
    export class SomeThing{

    }
}

var module: {copy module definition from node.d.ts}

if(module)
{
    module.exports = Organisation.Project.Entities;
}

I can use this code in my node application: 我可以在节点应用程序中使用以下代码:

var entities = require("../nodelibs/entities/entities");

var myThing = new entities.SomeClass();

despite there being both a entities.js and entities.d.ts at ../nodelibs/entities/entities there is no type info or compile time checking that SomeClass is a thing. 尽管存在既是entities.jsentities.d.ts../nodelibs/entities/entities没有类型信息或编译时检查SomeClass是一个东西。

I can manually import the references file and manually type the instance of the class but in my case SomeClass is a class with a lot of statics on it so I can't fingure out how to type it properly... 我可以手动导入引用文件并手动键入该类的实例,但是在我的情况下, SomeClass是一个上面有很多静电的类,因此我无法确定如何正确键入它。

var entities = require("../nodelibs/entities/entities");

var classRef = entities.SomeClass;

var myVar = classRef.staticProperty;

In this example I can't type classRef as SomeClass as it isn't an instance of SomeClass , it's the class constructor. 在此示例中,我不能将classRefSomeClass因为它不是SomeClass的实例,而是类构造函数。 How do I type classRef so that I can get access to the static properties on the class Some Class ? 如何键入classRef,以便可以访问类Some Class上的静态属性?

Thanks 谢谢

Write the code with ES6 modules 用ES6模块编写代码

In the module, use the static ES6 module syntax : 在模块中,使用静态ES6模块语法

// File entities.ts
export namespace Organisation.Project.Entities {
    export class SomeClass {
        public static abc = 'abc';
    }
}

Then, import your namespace: 然后,导入您的名称空间:

// File main.ts
import {Organisation} from './entities';
type classRef = Organisation.Project.Entities.SomeClass;
var myVar = classRef.abc;

Compile for Node.js 为Node.js编译

Say the compiler to generate modules in the CommonJS format: 说编译器以CommonJS格式生成模块:

// File tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs"
    },
    "exclude": [
        "node_modules"
    ]
}

Compile for the browser 为浏览器编译

In a browser, you can use a bundler or a loader. 在浏览器中,您可以使用捆绑器或装载器。 There is some configuration to do. 有一些配置要做。

Bundlers: 捆绑包:

Loaders: 装载机:

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

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