Sometimes, we want to create nested classes in TypeScript.
In this article, we’ll look at how to create nested classes in TypeScript.
How to create nested classes in TypeScript?
To create nested classes in TypeScript, we can create a static class property in our class.
For instance, we write
class Foo {
static Bar = class {};
}
const foo = new Foo();
const bar = new Foo.Bar();
to create the Foo class that has the static Bar property which is set to a class expression.
Then we can create a Bar instance with
const bar = new Foo.Bar();
Conclusion
To create nested classes in TypeScript, we can create a static class property in our class.