如何使用 node.js 在 Typescript 中的多个文件中拆分我的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21706455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:12:02  来源:igfitidea点击:

How do I split my module across multiple files in Typescript with node.js

typescript

提问by Luka Horvat

It seems that the information on how to actually structure code when writing Typescript is next to non-existent.

似乎在编写 Typescript 时如何实际构建代码的信息几乎不存在。

I want to make a server in node. It has external dependencies like socket.io. The server will be too big to put it all in one file (as I imagine is the case most of the time) so I figured I'd split it up. I want to have each class in a separate file and I want to be able to use them in the whole project without needing to do something crazy like

我想在 node.js 中创建一个服务器。它有像 socket.io 这样的外部依赖。服务器太大而无法将其全部放在一个文件中(正如我想象的大多数情况下是这种情况),所以我想我应该将其拆分。我想将每个类放在一个单独的文件中,并且我希望能够在整个项目中使用它们而无需做一些疯狂的事情

import vector = require("vector.ts");
var vec = new vector.Vector();

How do I do that? So far it seems that I'm fighting on two fronts. When I get tsc to actually compile, node complains on runtime, but when I modify the code so that node would work, it doesn't compile.

我怎么做?到目前为止,我似乎在两条战线上进行斗争。当我让 tsc 实际编译时,节点会在运行时抱怨,但是当我修改代码以使节点可以工作时,它不会编译。

I'd appreciate if someone could take the time to go through this step by step.

如果有人能花时间一步一步地完成这一步,我将不胜感激。

回答by Wolfgang

Actually you can (by now):

实际上你可以(现在):

file: class1.ts:

文件:class1.ts:

export class Class1 {
  name: string;

  constructor(name: string){
      this.name = name;
  }
}

file: class2.ts:

文件:class2.ts:

export class Class2 {
    name: string;
}

consolidating module file: classes.ts:

整合模块文件:classes.ts:

export { Class1 } from "./class1";
export { Class2 } from "./class2";

consuming file:

消费文件:

import { Class1, Class2 } from "./classes";

let c1 = new Class1("Herbert");
let c2 = new Class2();

In this manner you can have one class (or interface) per file. In one consolidating module file (classes.ts) you then reference all entities that make up your "module".

通过这种方式,您可以为每个文件创建一个类(或接口)。在一个合并模块文件 (classes.ts) 中,您可以引用构成“模块”的所有实体。

Now you only have to reference (import) on single module file to access all of your classes. You still have a neat compartmentalization between files.

现在您只需在单个模块文件上引用(导入)即可访问您的所有类。您仍然可以在文件之间进行巧妙的划分。

Hope this helps anyone still looking.

希望这可以帮助任何仍在寻找的人。

回答by Ryan Cavanaugh

Multi-file external modules are not supported yet in TypeScript.

TypeScript 尚不支持多文件外部模块。

For module structuring recommendations, this pagehas a good rundown of the options. In particular, if you have exactly one class per file, you can use export =so that the second line of code in your example would simply be var vec = new vector();, which would be reasonably straightforward.

对于模块结构建议,这个页面有一个很好的选项纲要。特别是,如果每个文件只有一个类,则可以使用,export =以便示例中的第二行代码只是var vec = new vector();,这将相当简单。

回答by DJ House

For anyone who stumbles across this thread (like me) who are trying to create one barrel file for external module declarations, loretoparisi's answer is the way to go. Here is my use case with example modules. I wanted to have a barrel declaration file (index.d.ts) for all my 3rd party declarations I was writing, but didn't want to put it 6 + module declarations in the same file. So this is what I did.

对于试图为外部模块声明创建一个桶文件的偶然发现此线程的任何人(如我),loretoparisi 的答案是要走的路。这是我的示例模块的用例。我想为index.d.ts我正在编写的所有 3rd 方声明提供一个桶声明文件 ( ),但不想将 6 + 模块声明放在同一个文件中。所以这就是我所做的。

I have a folder structure like this:

我有一个这样的文件夹结构:

src/ 
  index.ts

Example index.ts:

示例index.ts

import WildEmitter from 'wildemitter';
import SomethingCool from 'somethingcool';

// ... rest of file

I converted my folder structure like this to write declaration files for my imported 3rd party modules:

我像这样转换了我的文件夹结构来为我导入的第 3 方模块编写声明文件:

src/ 
  index.ts
  /types
    index.d.ts
    /modules
      wildemitter.d.ts
      somethingcool.d.ts

In my src/types/index.d.tsfile, I have:

在我的src/types/index.d.ts文件中,我有:

/// <reference path="modules/wildemitter.d.ts" />
/// <reference path="modules/somethingcool.d.ts" />

Then I can keep my module declarations in separate files.

然后我可以将我的模块声明保存在单独的文件中。

In src/types/modules/wildemitter.d.tsI have:

src/types/modules/wildemitter.d.ts我有:

declare module 'wildemitter' {
  // declarations
}

Hopefully this helps someone!

希望这对某人有所帮助!