Typescript: using a type of static inner class

I am trying to write a class that provides an internal data structure to its consumers, the data structure that the class will use.

Example:

class Outer {
    static Inner = class {
        inInner: number
    };

    constructor(public inner: Outer.Inner) { }
}

The problem is that it is Outer.Innernot recognized as a type.

The only answers I found were workarounds (on the Typescript playground) . I am wondering if there are any elegant ways to do this.


USECASE:

, WebSockets . Action Data. Data . ( ), , Action1, Action1Data, , Action1.Data .

+5
2

" " - :

class Outer {
    constructor(public inner: Outer.Inner) { }
}

namespace Outer {
    export class Inner {
        inInner: number
    };
}

// Spec
let outer1 = new Outer({ inInner: 3 });

let outerInner = new Outer.Inner();
let outer2 = new Outer(outerInner);

TypeScript : namespace, type value. namespace .

http://www.typescriptlang.org/docs/handbook/declaration-merging.html

, TypeScript " ".

, class - es5. ? ? Outer.prototype.???

, es5, :

var Outer = function Outer(...) { ... }
Outer.Inner = function Inner() { ... }

, Outer in Outer.Inner " " Inner.

+8

, , # -like. typeof. :

class Outer
{
    static Inner = class
    {
        inInner: number = 0;
    };

    constructor(public inner: typeof Outer.Inner.prototype) { }
}

typeof Outer.Inner.prototype , , Outer.Inner :

namespace Outer
{
    export type Inner = typeof Outer.Inner.prototype;
}

, , , :

class Outer
{
    static Inner = (() =>
    {
        @decoratable()
        class OuterInner
        {
            inInner: number = 0;
        }
        return OuterInner;
    })();

    constructor(public inner: Outer.Inner) { }
}

namespace Outer
{
    export type Inner = typeof Outer.Inner.prototype;
}
0

Source: https://habr.com/ru/post/1672593/


All Articles