27

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.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • there's a short definition for both a couple paragraphs in the link you have in your post. Basically namespaces are Typescripts way of implementing the module pattern. While the modules are now equivalent to the ES2015 modules. The declare part means both are expected to be present and defined by someone else. :) – toskv Jan 30 '17 at 09:51
  • @toskv, yes, I've certainly read the page I linked to. But it's very confusing, that's why I posted the question here – Max Koretskyi Jan 30 '17 at 10:05
  • @toskv, I've got correct answer, please see [my answer](http://stackoverflow.com/a/42030656/2545680) – Max Koretskyi Feb 03 '17 at 18:23
  • awesome! :) glad you could clear that out. – toskv Feb 03 '17 at 21:40

1 Answers1

17

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.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 3
    when to use `declare namespace xxx {}`? – Lin Du Jun 29 '17 at 07:51
  • @novaline, check [this](https://www.typescriptlang.org/docs/handbook/namespaces.html). Namespaces are essentially a `Revealing Module Pattern` in JS. You can ask a separate question for more details and reference it here – Max Koretskyi Jun 29 '17 at 08:36
  • 3
    I read the official doc, but the doc explain it not clearly. I am aware of the using of `declare module 'xxx' {}`. But still don't know when to use `declare namespace xxx {}`, and what's the relationship of `es6 module system` and global `script` tag? – Lin Du Jun 29 '17 at 10:51
  • @novaline, no problem, then ask a separate question. The community knows a lot more than me – Max Koretskyi Jun 29 '17 at 10:53
  • 1
    And the `export =`, and `export as namespace React`. Don't know when to use. If there are some demos to explain these, I will appreciate that! – Lin Du Jun 29 '17 at 10:55
  • @novaline, sorry, man, these are all questions that are not really related to the one I'm answer, ask separate questions, I'll take a look – Max Koretskyi Jun 29 '17 at 11:08
  • 5
    I don't think this answers the question. What is the difference between `declare module` and `declare namespace` ?? Do they do the same thing? Both can be used with and without quotes. – Max Heiber Apr 10 '19 at 08:55