Eric
Eric

Reputation: 97641

Referencing inner classes in typescript

I can successfully declare a nested class like this:

class Outer {
    static Inner = class Inner {

    };
}

However, I would like my outer class to hold some instances of my inner class:

class Outer {
    constructor() {
        this.inners = [new Outer.Inner()];
    }
    static Inner = class Inner {

    };

    inners: Array<Inner>; // this line errors
}

But this gives me error TS2304: Cannot find name 'Inner'.

How can I make this work?

Upvotes: 7

Views: 3577

Answers (1)

Bruno Grieder
Bruno Grieder

Reputation: 29874

Not sure this can be achieved this way however, as a workaround:

class Outer {
    inners: Array<Outer.Inner>;
}

namespace Outer {
    export class Inner {
    }
}

Note: the class must be defined before the namespace

See it in action

Upvotes: 2

Related Questions