Search code examples
javascripttypescript

Can you create nested classes in TypeScript?


Is there a way to nest classes in TypeScript. E.g. I'd like to use them like:

var foo = new Foo();
var bar = new Foo.Bar();

Solution

  • In modern TypeScript we have class expressions which you can use to create a nested class. For example you can do the following :

    class Foo {
        static Bar = class {
            
        }
    }
    
    // works!
    var foo = new Foo();
    var bar = new Foo.Bar();