Previous solutions regarding typescript and nested classes often recommended utilizing the declaration merging feature of the language. I've experimented with this approach using the code snippet below, which functions correctly but triggers a compiler error:
foo.ts(9,37): error TS2341: Property '_bar' is private and only accessible
within class 'Foo'.
...which is puzzling because Class Bletch is indeed a part of Foo.
Is there an optimal way to address the issue of accessing a private member from the outer class? (I am aware of the workaround involving (this._foo as any)
but I'm hoping for a more elegant solution.)
Example:
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());