ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[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 ``` /// <reference path = "IShape.ts" /> namespace Drawing { export class Circle implements IShape { public draw() { console.log("Circle is drawn"); } } } ``` TestShape.ts ``` /// <reference path = "IShape.ts" /> /// <reference path = "Circle.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(); ```