//命名空间中通过export将想要暴露的部分导出,否则无法读取其值 namespace a { exportconst Time: number = 1000 exportconst fn = <T>(arg: T): T => { return arg } fn(Time) } namespace b { exportconst Time: number = 1000 exportconst fn = <T>(arg: T): T => { return arg } fn(Time) } a.Time
嵌套命名
1 2 3 4 5 6 7 8 9 10 11 12
namespace a { exportnamespace b { exportclassVue{ parameters: string constructor(parameters: string) { this.parameters = parameters } } } } let v = a.b.Vue new v('1')
抽离命名空间
1 2 3 4 5 6 7 8 9 10 11
//a.ts exportnamespace A { exportconst a = 1 } //b.ts
interface Name { name: string } interface Age { age: number } interface Sex { sex: number } let people1: Name = { name: "小满" } let people2: Age = { age: 20 } let people3: Sex = { sex: 1 } const people = Object.assign(people1,people2,people3);//people 会被推断成一个交差类型 Name & Age & sex;