TypeScript 中的内部模块和外部模块有什么区别? [英] What's the difference between internal and external modules in TypeScript?

查看:32
本文介绍了TypeScript 中的内部模块和外部模块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间阅读 Typescript 语言规范,但对内部外部模块之间的区别有些困惑.以下是直接取自规范的描述:

I have spent some time reading the Typescript language specification and am somewhat confused about the difference between internal and external modules. Here is the description taken directly from the specification:

内部模块(第 9.2.2 节)是其他模块(包括全局模块和外部模块)的本地或导出成员.内部模块使用指定其名称和主体的 ModuleDeclarations 进行声明.具有多个标识符的名称路径等效于一系列嵌套的内部模块声明.

Internal modules (section 9.2.2) are local or exported members of other modules (including the global module and external modules). Internal modules are declared using ModuleDeclarations that specify their name and body. A name path with more than one identifier is equivalent to a series of nested internal module declarations.

外部模块(第 9.4 节)是使用外部模块名称引用的单独加载的代码体.外部模块被编写为包含至少一个导入或导出声明的单独源文件.此外,可以使用全局模块中的 AmbientModuleDeclarations 声明外部模块,直接将外部模块名称指定为字符串文字.这将在第 0 节中进一步描述.

External modules (section 9.4) are separately loaded bodies of code referenced using external module names. An external module is written as a separate source file that contains at least one import or export declaration. In addition, external modules can be declared using AmbientModuleDeclarations in the global module that directly specify the external module names as string literals. This is described further in section 0.

据我所知,我认为外部模块对应于 typescript 文件,而没有包含简单地导出一组类型和/或变量的模块定义.从另一个打字稿文件中,我可以使用 import foo = module("foo");

From what I've understood I think that external modules correspond to typescript files without enclosing module definitions that simply export a set of types and/or variables. From another typescript file I can simple import an external module in foo.ts with import foo = module("foo");

有人可以向我解释外部模块和内部模块之间的去向吗?

Can somebody explain to me the destinction between external and internal modules?

推荐答案

规范的 9.3 和 9.4 节更清楚地解释了这一点.我将在此处重现这些部分中给出的一些示例.

Sections 9.3 and 9.4 of the specification explain this more clearly. I'll reproduce here some of the examples given in those sections.

假设以下代码在main.ts中.

import log = module("log");
log.message("hello");

这个文件引用了一个外部模块 log,由 log.ts 导出的任何内容定义.

This file references an external module log, defined by whatever log.ts exports.

export function message(s: string) { 
  console.log(s); 
}

注意 log.ts 没有在任何地方使用 module 关键字.它只是用 export 导出东西.

Notice that log.ts doesn't use the module keyword anywhere. It just exports things with export.

这个文件有两个内部模块,X.Y.Z.

This file has two internal modules, X.Y.Z.

module A.B.C { 
  import XYZ = X.Y.Z; 
  export function ping(x: number) { 
    if (x > 0) XYZ.pong(x – 1); 
  }
} 
module X.Y.Z { 
  import ABC = A.B.C; 
  export function pong(x: number) { 
    if (x > 0) ABC.ping(x – 1); 
  } 
}

这些行为(大部分)类似于外部模块,但它们包含在一个文件中,您无需引用任何外部文件即可使用它们.定义它们时,它们必须包含在 module 块中.

These behave (mostly) like external modules, but they are contained in one file and you don't have to reference any outside files to use them. They have to be contained inside of a module block when they are defined.

这篇关于TypeScript 中的内部模块和外部模块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆