This project is read-only.

Clean syntax for nested classes

Topics: Language Specification
Apr 13, 2014 at 3:02 PM
In many object oriented languages, classes can be nested and accessed through a qualified name. This helps with keeping class-specific types that make little sense alone (without a class that uses them), as close as possible to that class.

Every function in Javascript is also a "class" constructor, so such classes can be implemented in Javascript too:
var A = (function () {
    var A = function () {
    A.B = function () {
    A.prototype = {
        C: (function () {
            function C() {
            return C;
    return A;

// usage:
var b = new A.B;
alert("b instanceof A.B: " + (b instanceof A.B));
var a = new A;
var c = new a.C;
alert("c instanceof a.C: " + (c instanceof a.C));
Therefore, nested classes are not only pretty or practical, but also a matter of consistency with existing language features.

Same can be said about enums (tables can be trivially included in a class).

Declaration merging is not a solution for this issue but merely a workaround, which can be seen in its resulting Javascript code. It's also not fully functional because it requires nested types to be exported from the module.

So I believe this syntax should be valid in Typescript:
class A{
    class B {} // nested class A.B
    private class C {} // nested class A.C accessible from within class A, otherwise visible from outside
    enum E {} // nested enum A.E

// and, optionally
module A{
    class D {} // secret class D visible from class A (but invisible from outside)
Apr 14, 2014 at 7:43 AM
There was an issue tracking this:

Perhaps it needs to be reopened and revisited. I agree that there's no good reason I can think of not to do this properly.