Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1015 Bytes

空间别名.md

File metadata and controls

46 lines (37 loc) · 1015 Bytes

空间别名

可以为命名空间或命名空间成员设置别名,这对于访问嵌套过深的命名空间成员的代码简化特别有用。别名关键字为 import:

namespace A {
  export namespace B {
    export namespace C {
      export let msg = 'hello world';
    }
  }
}

// import为子命名空间C定义别名
import N = A.B.C;
// 输出: hello world
console.log(N.msg);

//import为子命名空间C的成员msg定义别名
import m = A.B.C.msg;
//输出:hello world
console.log(m);

实际上,在当前的编译器版本中,import关键字完全可以替换为letconst,下面的代码和上面的等价:

namespace A {
  export namespace B {
    export namespace C {
      export let msg = 'hello world';
    }
  }
}

// let为子命名空间C定义别名
let N = A.B.C;
// 输出: hello world
console.log(N.msg);

//const为子命名空间C的成员msg定义别名
const m = A.B.C.msg;
//输出:hello world
console.log(m);