Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

typescript - What is the difference between `declare namespace` and `declare module`

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 odeindex.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...