Namespace keyword in TypeScript

460 views Asked by At

Trying to find out what does namespace keyword mean in TypeScript?

Here's some reshuffle they've done recently to use namespace instead of module:

https://github.com/Microsoft/TypeScript/commit/224e7630ea17c4547e97f85634f446b877955a67

In the absence of namespace spec, it looks like a rather pointless rename to make the language look C#-like.

Or is there an obscure little difference between the two? The word namespace sorta implies the shared scope/space between multiple declarations using the same name. Is that a correct intuition?

1

There are 1 answers

1
David Sherret On BEST ANSWER

You can read about it in the namespace keyword issue, but the basic idea is to differentiate between internal (namespace) and external (module) modules.

To better align with ES6 modules as suggested in #2159, this PR introduces a new namespace keyword as the preferred way to declare what was formerly known as "internal modules". Our terminology changes as follows:

  • Internal module becomes Namespace.
  • External module becomes Module.

For backwards compatibility, namespaces can still be declared in the old way (using the module keyword) and no existing code is broken by this change. We may consider introducing a compiler option that disallows using the module keyword to declare namespaces.

Source