HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

Can you create nested classes in TypeScript?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptyoucannestedclassescreate

Problem

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();

Code Snippets

class Foo {
    static Bar = class {
        
    }
}

// works!
var foo = new Foo();
var bar = new Foo.Bar();

Context

Stack Overflow Q#32494174, score: 237

Revisions (0)

No revisions yet.