typescript 有什么方法可以在打字稿中声明嵌套类结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13495107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:56:41  来源:igfitidea点击:

Any way to declare a nest class structure in typescript?

typescript

提问by Corey Alix

I'm interested in defining an existing framework (openlayers.d.ts) but cannot figure out how to express the fact that OpenLayers.Layer is both a class and a namespace for OpenLayers.Layer.Markers. I believe this makes Markers a nested class of Layer.

我对定义现有框架 (openlayers.d.ts) 感兴趣,但无法弄清楚如何表达 OpenLayers.Layer 既是类又是 OpenLayers.Layer.Markers 的命名空间这一事实。我相信这使 Markers 成为层的嵌套类。

usage:

用法:

l = new OpenLayers.Layer(...); // this is a base class, never do this
m = new OpenLayers.Layer.Markers(...);

How would you declare both the Layer and Markers class in typescript?

你将如何在打字稿中声明 Layer 和 Markers 类?

回答by sboisse

This seems like it has been fixed in versions 0.9.1.1 and later. You just have to create a module with the same name as the class where you want to nest types, and put your nested types in it.

这似乎已在 0.9.1.1 及更高版本中修复。您只需要创建一个与要嵌套类型的类同名的模块,然后将嵌套的类型放入其中。

More concretely, this is how you do it:

更具体地说,这就是你的做法:

declare module a
{
    class b
    {
    }

    module b
    {
        class c
        {
        }
    }
}

var myB = new a.b();
var myC = new a.b.c();

This works as well when nesting types in typescript code with the export keyword:

当使用 export 关键字在打字稿代码中嵌套类型时,这也适用:

export module a
{
    export class b
    {
    }

    export module b
    {
        export enum c
        {
            C1 = 1,
            C2 = 2,
            C3 = 3,
        }
    }
}

As mentioned by the user @recursive in the comments below, the order of declaration is important. So the class definition must be located before the module with the nested types.

正如用户@recursive 在下面的评论中提到的,声明的顺序很重要。因此类定义必须位于具有嵌套类型的模块之前。

回答by Fenton

You can use namespace/class merging to get an equivalent effect to nested classes. This example is adapted from Chapter 1 of Pro TypeScript.

您可以使用命名空间/类合并来获得与嵌套类等效的效果。此示例改编自 Pro TypeScript 的第 1 章。

You perform merging with a class and a namespace, or with a function and a namespace.

您执行与类和命名空间或函数和命名空间的合并。

In all cases, the namespace must appear after the class or function for the merge to work.

在所有情况下,命名空间必须出现在类或函数之后才能进行合并。

Adapted example:

改编的例子:

class Outer {

}

namespace Outer {
    export class Mid {

    }

    export module Mid {
        export class Inner {

        }
    }
}

var a = new Outer();
var b = new Outer.Mid();
var x = new Outer.Mid.Inner();

回答by MarcG

As of 2016, I guess this is easier:

截至 2016 年,我想这更容易:

class A {
    static B = class { }
}

var a = new A();
var b = new A.B();

回答by Leng

This is a bit late but others may find it useful. This is how I did it to fool TS to accomplish the task.

这有点晚了,但其他人可能会发现它很有用。这就是我欺骗TS完成任务的方式。

declare module OpenLayers {
  interface Layer { 
   ....
  }

  interface Markers {
    ....
  }

  var Layer: {
    new(name:string, options?:any):Layer;
    prototype:Layer;
    Markers: { 
      new(params?:any):Markers;
      prototype: Markers;
    }
  }
}

var markers = new OpenLayers.Layer.Markers();

回答by user1793714

Currently there is no support for nested classes.

目前不支持嵌套类。

http://typescript.codeplex.com/workitem/460

http://typescript.codeplex.com/workitem/460

回答by Vad

Use TypeScript's class expressionsfor this:

为此使用 TypeScript 的类表达式

var OpenLayers = class {
    static Layer = class {
        static Markers = class {}
    }
}