Access private property in Typescript
# Why
There are rare occasions when writing unit tests where I need to verify the value of a private property on a Typescript object instance.
# Remove the type checking on the instance
Imagine SomeObject
has a private property called PrivateProp
. We can skip
the type checking using any
type assertion.
it('equals some value', () => { const someObject = new SomeObject(); expect((<any>someObject).privateProp).to.eq 'some value'; });
This could also be expresses as (someObject as any)
See also This SO answer.