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

After reading this guide and this quote:

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

It seemed to me that the declare module no longer used and is being replaced by declare namespace , however, when studying node_modules\@types\node\index.d.ts I see that both the declare module and declare namespace :

 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 is that? Who cares?

External modules (ES6 modules) do not work , as I understand it.

+4
source share
1 answer

There are two ways to specify modules in TS:

 declare module "buffer" {} // with quotes 

and

 declare module buffer {} // without quotes 

The first (with quotation marks) means an external module (ES6 module) and is currently used in .d.ts files to place several ES6 modules in a single file:

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

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

 declare namespace buffer {} 

So in this quote:

It is 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 more details.

+3
source

Source: https://habr.com/ru/post/1263565/


All Articles