Why are some Typescript module names given as strings?

后端 未结 2 1217
情歌与酒
情歌与酒 2021-01-21 06:47

Typically, Typescript modules are declared like

declare module MyModule {
     ....
}

but sometimes I also see it like

declare         


        
2条回答
  •  盖世英雄少女心
    2021-01-21 07:08

    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.

提交回复
热议问题