命名空间

最后更新于:2022-04-02 03:28:22

[TOC] ## 概述 ``` namespace span{ /*需要用 export 导出*/ export class demo{ foo(name:string):string{ return name; } } } let demo = new span.demo; demo.foo("abc") ``` ## 多个文件中有相同的命名空间 IShape.ts ``` namespace Drawing { export interface IShape { draw(); } } ``` Circle.ts ``` /// namespace Drawing { export class Circle implements IShape { public draw() { console.log("Circle is drawn"); } } } ``` TestShape.ts ``` /// /// function drawAllShapes(shape:Drawing.IShape) { shape.draw(); } drawAllShapes(new Drawing.Circle()); ``` 编译执行 `tsc --out app.js TestShape.ts ` ## 嵌套命名空间 ``` namespace demo1{ export namespace demo2{ export class demo3{ demo4(){ console.log("hello"); } } } } let demo3 = new demo1.demo2.demo3(); demo3.demo4(); ```
';