typescript wrap Array Generic type

时间:2015-09-14 16:09:24

标签: arrays generics typescript

I'd like to wrap the generic Array in typescript. We have code as below:

    _bindings: Array<BindingDescription>;

What I want is to have a wrapper around Array so I can use

    _bindings: BindingDescriptionCollection;

I'm trying to wrap it as below:

    export class BindingDescriptionCollection implements Array<BindingDescription> {}

But it need to implements all the functions and properties in Array.

Is there any easier way?

2 个答案:

答案 0 :(得分:4)

您是要添加其他功能还是仅使用更干净的名称?如果是后者,您可以使用type alias

type BindingDescriptionCollection = Array<BindingDescription>;

答案 1 :(得分:2)

  

但它需要实现Array中的所有函数和属性。   有没有更简单的方法?

您可以使用extend继承功能:

interface BindingDescription{}
class BindingDescriptionCollection extends Array<BindingDescription> {}