7

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?

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Possible duplicate of [Can you create nested classes in TypeScript?](http://stackoverflow.com/questions/32494174/can-you-create-nested-classes-in-typescript) – JB Nizet Oct 01 '16 at 14:46
  • Not a duplicate - this successfully creates the nested class, but the issue is with creating a member that has a nested class type. – Eric Oct 01 '16 at 14:48
  • Both the question and the accepted answer show how to reference the inner type: `var bar = new Foo.Bar();`. – JB Nizet Oct 01 '16 at 14:57
  • 2
    They show how to _create an instance_ of the inner type. I wish to _declare a member variable_ as being of that inner type. – Eric Oct 01 '16 at 15:01
  • 1
    this doesn't error `inners: Array` – marzelin Oct 01 '16 at 15:10
  • @marzelin: I get _"Type 'Inner' is not assignable to type 'typeof Inner'"_ – Eric Oct 01 '16 at 16:23
  • @marzelin: See [code example](https://www.typescriptlang.org/play/#src=class%20Outer%20%7B%0D%0A%20%20%20%20constructor()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.inners%20%3D%20%5Bnew%20Outer.Inner()%5D%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20static%20Inner%20%3D%20class%20Inner%20%7B%0D%0A%0D%0A%20%20%20%20%7D%3B%0D%0A%0D%0A%20%20%20%20inners%3A%20Array%3Ctypeof%20Outer.Inner%3E%3B%20%2F%2F%20this%20line%20errors%0D%0A%7D) – Eric Oct 01 '16 at 16:39
  • @Eric maybe something like [this](https://www.typescriptlang.org/play/index.html#src=class%20Outer%20%7B%0D%0A%0D%0A%20%20%20%20static%20Inner%20%3D%20class%20Inner%20%7B%0D%0A%20%20%20%20%20%20%20%20inInner%3A%20number%0D%0A%20%20%20%20%7D%3B%0D%0A%0D%0A%20%20%20%20inners%20%3D%20%5Bnew%20Outer.Inner()%5D%20%2F%2F%20this%20line%0D%0A%7D%0D%0A%0D%0Aconst%20outer%20%3D%20new%20Outer()%0D%0Aouter.inners%5B0%5D%20%2F%2F%20has%20type%20Inner)? – marzelin Oct 01 '16 at 16:57

1 Answers1

2

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

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • Does this mean the class constructor is unable to reference `Outer.Inner`? – Eric Oct 01 '16 at 15:06
  • Yes. I guess this is because an Inner class is defined outside (after) the constructor where as properties such as `inner` are defined inside the constructor. See [the compiled javascript](https://www.typescriptlang.org/play/#src=%0D%0Aclass%20Outer%20%7B%0D%0A%0D%0A%20%20%20%20static%20Inner%20%3D%20class%20Inner%20%7B%0D%0A%0D%0A%20%20%20%20%7D%3B%20%2F%2Foutside%20the%20constructor%0D%0A%0D%0A%20%20%20%20astring%3A%20string%20%3D%20%22defined%20inside%20the%20constructor%22%0D%0A%0D%0A%20%20%20%20inners%3A%20Array%3COuter.Inner%3E%3B%20%2F%2F%20this%20line%20errors%0D%0A%7D) – Bruno Grieder Oct 01 '16 at 15:13
  • The `Outer`var is not yet defined (inside the constructor). – Bruno Grieder Oct 01 '16 at 15:16
  • 1
    [This seems to work](https://www.typescriptlang.org/play/#src=class%20Outer%20%7B%0D%0A%20%20%20%20constructor()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.inners%20%3D%20%5Bnew%20Outer.Inner()%5D%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20inners%3A%20Array%3COuter.Inner%3E%3B%20%2F%2F%20this%20line%20errors%0D%0A%7D%0D%0Anamespace%20Outer%20%7B%0D%0A%20%20%20%20export%20class%20Inner%20%7B%0D%0A%20%20%20%20%7D%0D%0A%7D) – Eric Oct 01 '16 at 16:42
  • @Eric Yes it does; it is similar to the one linked in my response: [in action](https://www.typescriptlang.org/play/#src=class%20Outer%20%7B%0D%0A%20%20%20%20inners%3A%20Array%3COuter.Inner%3E%20%3D%20%5Bnew%20Outer.Inner()%5D%0D%0A%7D%0D%0A%0D%0Anamespace%20Outer%20%7B%0D%0A%20%20%20%20export%20class%20Inner%20%7B%0D%0A%20%20%20%20%20%20%20%20sayHello()%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20window.alert('hello')%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Anew%20Outer().inners%5B0%5D.sayHello()) – Bruno Grieder Oct 01 '16 at 16:54