Why are some Typescript module names given as strings?

后端 未结 2 1219
情歌与酒
情歌与酒 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:29

    As the offical docs described, now

    "Internal modules" === “namespaces”

    “External modules” === “modules”

    A note about terminology: 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”, as to align with ECMAScript 2015’s terminology, (namely that module X { is equivalent to the now-preferred namespace X {).

    Here, I will show you some demos codes, may be it can make this more easy to understand!

    /*
    Note: All of these files in the same folder, i.e. the same path!
    */
    
    // declare a namespace, demoNamespace.d.ts
    declare namespace "DemoNamespace" {
        export function fn(): string;
    }
    
    // using a namespace, demoNamespaceModule.ts
    import * as n from "demoNamespace";
    
    //let f = new n.DemoNamespace.fn(); ???
    
    
    // export a module, demoModule.ts
    export class Alpha { /* ... */ }
    export class Beta { /* ... */ }
    
    // using a module, demoModuleTest.ts
    import * as m from "./demoModule";
    
    let a = new m.Alpha();
    let b = new m.Beta();
    

    references links:

    namespaces-and-modules namespaces modules

提交回复
热议问题