我一直在使用Basarats优秀的Collections库,为0.9.0创建类型,例如:
Dictionary<ControlEventType,
Dictionary<number, (sender: IControl,
eventType: ControlEventType,
order: ControlEventOrder,
data: any) => void >>
Run Code Online (Sandbox Code Playgroud)
现在我不想每次使用它时都要完整地写这个.似乎有效的方法之一是:
export class MapEventType2Handler extends C.Dictionary<ControlEventType,
C.Dictionary<number,
(sender: IControl,
eventType: ControlEventType,
order: ControlEventOrder,
data: any) => void >> {}
Run Code Online (Sandbox Code Playgroud)
我可以写:
EH2: MapEventType2Handler = new MapEventType2Handler();
Run Code Online (Sandbox Code Playgroud)
代替:
EH: Dictionary<ControlEventType,
Dictionary<number,
(sender: IControl,
eventType: ControlEventType,
order: ControlEventOrder,
data: any) => void >>;
Run Code Online (Sandbox Code Playgroud)
有人遇到过更好的想法吗?
我也正在试验'typedeffing'各种功能签名而没有很好的结果.
spl*_*tor 32
从版本1.4开始,Typescript支持类型别名(源代码,另请参阅此答案):
type MapEventType2Handler = Dictionary<ControlEventType,
Dictionary<number,
(sender: IControl,
eventType: ControlEventType,
order: ControlEventOrder,
data: any) => void >>;
Run Code Online (Sandbox Code Playgroud)
首先感谢您的客气话:)。
您的解决方案实际上是最佳的。
长答案 Typescript 有两个声明空间。类型和变量。
将项引入类型声明空间的唯一方法是通过类或接口(0.8.x 也可以使用模块来引入类型。它已从 0.9.x 中删除)
接口将不起作用,因为您希望实现保持完整(并且接口与实现无关)。
变量将不起作用,因为它们没有在类型声明空间中引入名称。它们仅在变量声明空间中引入名称。
例如:
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;
Run Code Online (Sandbox Code Playgroud)
如果该语言有宏,则可以完成您想要的操作,但目前类+扩展是唯一的方法。
| 归档时间: |
|
| 查看次数: |
13323 次 |
| 最近记录: |