Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 345
Max Koretskyi Avatar asked Jan 30 '17 09:01

Max Koretskyi


People also ask

What is difference between namespace and module in Python?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.

What is declare namespace?

Namespace Declaration We can create a namespace by using the namespace keyword followed by the namespace_name. All the interfaces, classes, functions, and variables can be defined in the curly braces{} by using the export keyword. The export keyword makes each component accessible to outside the namespaces.

Is namespace a module?

Using Namespaces Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .

What does declare module do?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...


1 Answers

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.

like image 73
Max Koretskyi Avatar answered Sep 29 '22 01:09

Max Koretskyi