Categories
TypeScript Answers

How to create nested classes in TypeScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *