导入 TypeScript 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18556187/
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:08:37  来源:igfitidea点击:

Importing TypeScript modules

typescript

提问by jax

I am just trying to get my head around TypeScript,

我只是想了解一下 TypeScript,

Say I have a module animals.tslike this:

假设我有一个animals.ts这样的模块:

export module Animals {

    export interface Animal {
        name(): void;
    }

    export class Elephant implements Animal {

        constructor() {

        } 

        public name() {
            console.log("Elephant");
        }
    }

    export class Horse implements Animal {

        constructor() {

        }

        public name() {
            console.log("Horse");
        }
    }
}

And I want to use this module in another file animals_panel.ts:

我想在另一个文件中使用这个模块animals_panel.ts

import animals = require("animals")

module AnimalPanel {

    var animal = new animals.Animals.Elephant();
    animal.name();
}
  1. It seems a bit weird to me that I have to use animals.Animals.Elephant(), I would have expected Animals.Elephant(). Am I doing something wrong or is this the correct behaviour?
  2. is it possible to import import animals = require("animals")inside the AnimalPanelmodule (I get errors when I try to do this)?
  1. 对我来说,我必须使用它似乎有点奇怪animals.Animals.Elephant(),我会预料到的Animals.Elephant()。我做错了什么还是这是正确的行为?
  2. 是否可以import animals = require("animals")AnimalPanel模块内部导入(尝试执行此操作时出现错误)?

回答by basarat

When you are using external modules each file is a module. So declaring a local internal module within a file e.g. export module Animals {leads to unnecessary double indirection.

当您使用外部模块时,每个文件都是一个模块。因此,例如在文件中声明本地内部模块export module Animals {会导致不必要的双重间接访问。

I would code animals.ts as :

我会将animals.ts编码为:

export interface Animal {
    name(): void;
}

export class Elephant implements Animal {

    constructor() {

    } 

    public name() {
        console.log("Elephant");
    }
}

export class Horse implements Animal {

    constructor() {

    }

    public name() {
        console.log("Horse");
    }
}

And then use it as :

然后将其用作:

import animals = require("animals")

module AnimalPanel {

    var animal = new animals.Elephant();
    animal.name();
}

PS: a video on this subject of internal / external typescript modules : http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

PS:关于内部/外部打字稿模块这个主题的视频:http: //www.youtube.com/watch?v=KDrWLMUY0R0&hd =1

回答by Alex Filatov

You can use 2 types of syntaxes export/import:

您可以使用两种类型的语法export/import

  1. (AMD style) Requiresyntax which supported in ES5:

    var animals = require("animals");

  2. Use importstyle which started suppurts from ES6:

    import { Elephant, Horse } from "animals";

  1. RequireES5 支持的(AMD 风格)语法:

    var animals = require("animals");

  2. 使用import从 ES6 开始支持的样式:

    import { Elephant, Horse } from "animals";

TypeScript supports export =to model the traditional CommonJSand AMDworkflow. So both variants will works and I suggest to use 2nd because it more powerful mechanism.

TypeScript 支持export =对传统CommonJSAMD工作流进行建模。所以这两种变体都可以工作,我建议使用 2nd,因为它更强大的机制。

More details about it can be found on official the TypeScript Modules web page.

可以在官方 TypeScript 模块网页上找到有关它的更多详细信息。