如何将typescript内部模块与AMD模块一起使用

如何将typescript内部模块与AMD模块一起使用,typescript,amd,webpack,Typescript,Amd,Webpack,我不确定是否我的打字脚本结构不正确,因此可能在这里问错了问题 我在同一个文件夹中的不同文件中有2个相关的类1接口 我将它们包装在一个模块中,因为这感觉像是我应该从C语言开始做的事情 这都是angularjs,所以它有自己的DI,这可能很重要,但可能不是 文件1: export module Module1{ export interface IService{ } } 文件2: export module Module1{ export class Implementa

我不确定是否我的打字脚本结构不正确,因此可能在这里问错了问题

我在同一个文件夹中的不同文件中有2个相关的类1接口

我将它们包装在一个模块中,因为这感觉像是我应该从C语言开始做的事情

这都是angularjs,所以它有自己的DI,这可能很重要,但可能不是

文件1:

export module Module1{
    export interface IService{
    }
}
文件2:

export module Module1{
    export class Implementation implements IInterface{
    ...
    }
}
文件3是使用IInterface的角度注入实例的角度代码。如果我使用require./File2导入File2,它可以工作,但我更愿意导入整个Module1,如下所示,因此我不必单独要求每个类,因为这显然是一个简化的情况

import authModule = require('Module1');

var assetStreamApp = angular.module("[])
    .run(['IInterface'], (instance: IInterface) => {
        instance.doSomething();
    });
这可能吗

我不希望必须单独导入每个文件,然后为每个模块选择一个不同的别名来命名类型的名称空间,因为我觉得我应该能够这样做一次


编辑:在多读一点之后,我想我已经掌握了一些术语。我想在项目中使用typescript内部模块,但也使用AMD模块作为拆分点,这样我就可以使用webpack的代码拆分。

理想情况下,您应该只使用外部模块,而不要将内部模块与外部模块混合使用

这一点已经过详细讨论和讨论

我建议你做。。。IService.ts:

interface IService {
}

export = IService;
interface IService {
}

export default IService;
实施.ts:

import IInterface = require("./IInterface");

class Implementation implements IInterface{
...
}

export = Implementation;
import IInterface from "./IInterface";

export default class Implementation implements IInterface {
...
}
然后将它们适当地导入到您的文件中:

import IService = require("./IService");
import Implementation = require("./Implementation");

// use IService and Implementation here
将多个模块组合成一个模块

也就是说,如果您真的愿意,可以使用上面的IService.ts和Implementation.ts,然后创建一个名为Module1.ts的文件,在该文件中导入然后导出模块,如下所示:

export import IService = require("./IService");
export import Implementation = require("./Implementation");
然后在代码中,您可以像这样使用它:

import Module1 = require("./Module1");

// use Module1.IService or Module1.Implementation here
将多个模块与ES6模块相结合

顺便说一下,我想指出,如果您使用ES6模块,那么这样做非常方便

IService.ts:

interface IService {
}

export = IService;
interface IService {
}

export default IService;
实施.ts:

import IInterface = require("./IInterface");

class Implementation implements IInterface{
...
}

export = Implementation;
import IInterface from "./IInterface";

export default class Implementation implements IInterface {
...
}
模块1.ts:

// re-export the modules
export {default as IService} from "./IService";
export {default as Implementation} from "./Implementation";
然后,当你使用这个模块时,你可以很容易地从中获得你想要的东西。以下是一些导入语句示例:

// import only IService
import {IService} from "./Module1";
// import IService and Implementation
import {IService, Implementation} from "./Module1";
// or implement everything on a Module1 object
import * as Module1 from "./Module1";

谢谢,我来看看那些链接。它与导出接口iSeries设备有什么区别?@BenCr export interface iSeries设备将它作为模块的属性导出,而不是作为模块本身。所以通过使用export=IService和import-IService,那么IService就是实际的接口;然后到处使用module.IService。太好了,谢谢