CodyBugstein CodyBugstein - 1 month ago 10x
TypeScript Question

Why are some Typescript module names given as strings?

Typically, Typescript modules are declared like

declare module MyModule {

but sometimes I also see it like

declare module "MyModule" {

I am wondering what is the purpose of giving the name as a string?

I checked in the Typescript docs and found

This is a common pattern for modules whose imported entities are callable functions

I tried compiling both
and they compile to the same thing. Does this mean that with or without quotes are equivalent, and deciding to use them is just like using capital letters for class names, or starting interface names with


As outlined in the Typescript docs, there is a difference between the two styles. module x { is equivalent to namespace x {, while module "x" { is unlike either of the other two. This is definitely confusing, which is why the documentation using the latter syntax in all cases.

The question then must be asked, what is the difference between a namepsace and a so-called external module (i.e., a module that is declared with quotes)?

We get some insight from the docs:

Just like namespaces, modules can contain both code and declarations. The main difference is that modules declare their dependencies.

Modules also have a dependency on a module loader (such as CommonJs/Require.js).

Essentially, using the quotes indicates that it is indeed a module in the traditional Javascript sense that requires explicit import to be used. Without the quotes, you just create a global object that can be referenced and utilized without any explicit requires.