TypeScript-命名空间
小开开 2020-12-03 17:18:10 2020-12-03 41 0
命名空间
语法
namespace 名字 { // ...}
命名空间引入了新的作用域,大括号可以包含任意合法的代码。要在命名空间之外访问命名空间之内的成员,必须使用 export
关键字:
// 定义命名空间 nsnamespace ns { export let a = 'hello world'; let b = 1; // 正确,可以访问b,因为函数show和变量b在同一个命名空间之内 function show(){ console.log(b); }}// 正确,a被export,可以在ns之外访问let c: string = ns.a;// 错误,b不允许在ns之外访问// error TS2339: Property 'b' does not exist on type 'typeof ns'let d: number = ns.b;
对命名空间成员的访问,类似对象成员的访问,都是用点号运算符
.
- 作者:joye
- 出处:https://github.com/joye61