typescript typedef 就像 C/C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17164078/
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-10-21 02:49:30  来源:igfitidea点击:

typedef just like C/C++

typescript

提问by andyks

I've been using Basarats's excellent Collectionslibrary slightly updated for 0.9.0 creating types such as:

我一直在使用 Basarats 的优秀Collections库,针对 0.9.0 创建类型进行了轻微更新,例如:

Dictionary<ControlEventType, 
    Dictionary<number, (sender: IControl, 
                        eventType: ControlEventType, 
                        order: ControlEventOrder, 
                        data: any) => void >>

Now I don't want to have to write this in full every time I use it. One of the approaches which seems to work is:

现在我不想每次使用它时都必须完整地编写它。似乎有效的方法之一是:

export class MapEventType2Handler extends C.Dictionary<ControlEventType,
                                                C.Dictionary<number,
                                                (sender: IControl,
                                                 eventType: ControlEventType,
                                                 order: ControlEventOrder,
                                                 data: any) => void >> {}

I can then write:

然后我可以写:

EH2: MapEventType2Handler = new MapEventType2Handler();

instead of:

代替:

EH: Dictionary<ControlEventType, 
        Dictionary<number, 
        (sender: IControl, 
         eventType: ControlEventType, 
         order: ControlEventOrder, 
         data: any) => void >>;

Anyone come across any better ideas?

有人遇到过更好的想法吗?

I am also experimenting with 'typedeffing' various function signatures without great results.

我也在尝试对各种函数签名进行“类型定义”,但没有很好的结果。

回答by splintor

From version 1.4 Typescript supports type aliases (source, see also this answer):

从 1.4 版开始,Typescript 支持类型别名(来源,另请参阅此答案):

type MapEventType2Handler = Dictionary<ControlEventType, 
    Dictionary<number, 
    (sender: IControl, 
     eventType: ControlEventType, 
     order: ControlEventOrder, 
     data: any) => void >>;

回答by basarat

First off thanks for the kind words :).

首先感谢您的客气话:)。

Your solution is actually optimal.

您的解决方案实际上是最佳的。

Long answerTypescript has two Declaration spaces. Types and Variables.

长答案Typescript 有两个声明空间。类型和变量。

The only way to introduce items into the type declaration space is via a class or an interface ( 0.8.x could use module to introduce a type as well. Its removed from 0.9.x)

将项引入类型声明空间的唯一方法是通过类或接口(0.8.x 也可以使用模块来引入类型。它从 0.9.x 中删除)

Interface will not work since you want the implementation to stay intact (and interface is implementation independent).

接口将无法工作,因为您希望实现保持完整(并且接口与实现无关)。

Variables will not work since they do not introduce a name in the Type Declaration space. They only introduce a name in the variable declaration space.

变量将不起作用,因为它们不会在类型声明空间中引入名称。它们只在变量声明空间中引入一个名称。

e.g.:

例如:

class Foo {    
}

// Valid since a class introduces a Type AND and Variable
var bar = Foo; 

// Invalid since var introduces only a variable so bar cannot be used as a type
// Error: Could not find symbol. Since compiler searched the type declaration space 
var baz: bar; 

// Valid for obvious reasons 
var x: Foo; 

What you want could be done if the language had a macros but for now class+extends is the only approach.

如果语言有宏,你想要的就可以完成,但现在 class+extends 是唯一的方法。