使用commonjs导出和导入多个文件

时间:2016-03-16 01:11:08

标签: typescript

这是我的第一个名为“thing”的库:

//animals.ts
module Animals {
    export class Cat {
        public name = "cat";
    }

    export class Dog {
        public name = "dog";
    }
}

export = Animals;


//houses.ts
module Houses {
    export class Big {
        public name = "big";
    }

    export class Small {
        public name = "small";
    }
}

export = Houses;

现在我想将这两个模块导入第二个库。 我该如何设置我的第一个库的package.json? 什么应该是主要和打字价值?

我创建了一个名为index.ts的新文件:

import Animals = require("./animals");
import Houses = require("./houses");

export var animals = Animals;
export var houses = Houses;

然后我在第二个库中导入它:

import { animals } from "things";

这是对的吗?什么是解决这个问题的最佳方法?

1 个答案:

答案 0 :(得分:1)

而不是:

import Animals = require("./animals");
import Houses = require("./houses");

export var animals = Animals;
export var houses = Houses;

我愿意:

export import Animals = require("./animals");
export import Houses = require("./houses");

因为这会在变量类型声明空间(enter image description here)中公开它。除此之外