You could assert Foo
as any
:
class Foo {
private constructor(private x: number) {}
}
const instance = new (Foo as any)(5) as Foo;
console.log(instance);
But perhaps you might just want to create a static method that constructs an instance with a clear name that it's for testing:
class Foo {
private constructor(private x: number) {}
/** @internal */
static createForTesting(x: number) {
return new Foo(x);
}
}
const instance = Foo.createForTesting(5);
Note: The /** @internal */
excludes the method declaration from appearing in declaration files.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…