【发布时间】:2016-08-21 06:38:25
【问题描述】:
过去发布的关于打字稿和嵌套类的答案建议使用该语言的声明合并功能。我已经用下面的示例进行了尝试,它按预期执行,但会生成编译器消息:
foo.ts(9,37): error TS2341: Property '_bar' is private and only accessible
within class 'Foo'.
...这似乎很奇怪,因为正如所写,Class Bletch 是 Foo 的成员。
是否有一种最佳实践方法来抑制有关访问外部类的私有成员的错误?(我知道我可以将this._foo 替换为(this._foo as any) ,但似乎应该有更优雅的方式......)
例子:
export class Foo {
constructor( private _bar: number ){}
//...
}
export module Foo {
export class Bletch {
constructor( private _foo: Foo ) {}
barf(): number { return this._foo._bar; }
}
}
let a = new Foo(57);
let b = new Foo.Bletch(a)
console.log(b.barf());
【问题讨论】:
标签: typescript nested