`declare namespace` 和 `declare module` 有什么区别 [英] What is the difference between `declare namespace` and `declare module`

查看:806
本文介绍了`declare namespace` 和 `declare module` 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读本手册和此引述后:<块引用>

需要注意的是,在 TypeScript 1.5 中,命名法有改变了.内部模块"现在是命名空间".外部模块"现在只是模块"

我的印象是 declare module 不再使用,而是由 declare namespace 代替,但是在探索 node_modules\@types\node\index.html 时.d.ts 我可以看到 declare moduledeclare namespace 都被使用了:

 声明命名空间 NodeJS {导出 var 控制台:{原型:控制台;new(stdout: WritableStream, stderr?: WritableStream): 控制台;}...声明模块缓冲区"{导出变量 INSPECT_MAX_BYTES:数字;var BuffType:缓冲区类型;var SlowBuffType:SlowBuffer 类型;export { BuffType 作为缓冲区,SlowBuffType 作为 SlowBuffer };}

为什么会这样?有什么区别?

根据我的理解,外部模块(ES6 模块)不要在这里起作用.

解决方案

在 TS 中有两种指定模块的方式:

declare module "buffer" {}//带引号

declare module buffer {}//不带引号

前者(带引号)表示外部模块(ES6 模块),目前在 .d.ts 文件中用于将多个 ES6 模块放在一个文件中:

声明模块缓冲区"{}声明模块fs"{}

后者(不带引号)用作命名空间,现在替换为

声明命名空间缓冲区{}

所以,在这句话中:

<块引用>

需要注意的是,在 TypeScript 1.5 中,命名法有改变了.内部模块"现在是命名空间".外部模块"现在只是模块"

内部模块"是没有引号的模块,因为它们在 1.5 之前使用.

有关其他详细信息,请参阅此问题.

After reading this manual and this quote:

It’s important to note that in TypeScript 1.5, the nomenclature has changed. "Internal modules" are now "namespaces". "External modules" are now simply "modules"

I was under impression that declare module is no longer used and is replaced by declare namespace, however when exploring node_modules\@types\node\index.d.ts I can see that both declare module and declare namespace is used:

declare namespace NodeJS {
    export var Console: {
        prototype: Console;
        new(stdout: WritableStream, stderr?: WritableStream): Console;
    }
...

declare module "buffer" {
    export var INSPECT_MAX_BYTES: number;
    var BuffType: typeof Buffer;
    var SlowBuffType: typeof SlowBuffer;
    export { BuffType as Buffer, SlowBuffType as SlowBuffer };
}

Why so? What's the difference?

External modules (ES6 modules) do not come into play here as I understand.

解决方案

There are two ways of specifying modules in TS:

declare module "buffer" {} // with quotes

and

declare module buffer {} // without quotes

The former (with quotes) signifies external module (ES6 module) and is currently used in .d.ts files to put several ES6 modules in one file:

declare module "buffer" {}
declare module "fs" {}

The latter (without quotes) was used as namespace and is now replaced with

declare namespace buffer {}

So, in this quote:

It’s important to note that in TypeScript 1.5, the nomenclature has changed. "Internal modules" are now "namespaces". "External modules" are now simply "modules"

"Internal modules" are modules without quotes as they were used before 1.5.

See this issue for additional details.

这篇关于`declare namespace` 和 `declare module` 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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