zspitz / ts-activex-gen

Library + UI for generating Typescript definitions / DefinitelyTyped packages from COM type libraries / LibreOffice Doxygen XML
MIT License
12 stars 4 forks source link

Define collection types as extending a Collection interface #84

Open zspitz opened 6 years ago

zspitz commented 6 years ago

e.g.

declare namespace Word {
    class GroupShapes {
        private 'Word.GroupShapes_typekey': GroupShapes;
        private constructor();
        readonly Application: Application;
        readonly Count: number;
        readonly Creator: number;
        Item(Index: any): Shape;
        readonly Parent: any;
        Range(Index: any): ShapeRange;
    }
}

could be defined as:

declare class CollectionBase<TItem, TKey = number> {
    readonly Count: number;
    Item(Index: TKey): TItem;
}

declare namespace Word {
    class Shape {
        private 'Word.Shape_typekey': Shape;
        private constructor();
    }

    class GroupShapes extends CollectionBase<Shape, any> {
        private 'Word.GroupShapes_typekey': GroupShapes;
        private constructor();
        readonly Creator: number;
        readonly Parent: any;
    }
}

[Playground](http://www.typescriptlang.org/play/index.html#src=declare%20class%20CollectionBase%3CTItem%2C%20TKey%20%3D%20number%3E%20%7B%0D%0A%20%20%20%20readonly%20Count%3A%20number%3B%0D%0A%20%20%20%20Item(Index%3A%20TKey)%3A%20TItem%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20namespace%20Word%20%7B%0D%0A%20%20%20%20class%20Shape%20%7B%0D%0A%20%20%20%20%20%20%20%20private%20'Word.Shape_typekey'%3A%20Shape%3B%0D%0A%20%20%20%20%20%20%20%20private%20constructor()%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20class%20GroupShapes%20extends%20CollectionBase%3CShape%2C%20any%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20private%20'Word.GroupShapes_typekey'%3A%20GroupShapes%3B%0D%0A%20%20%20%20%20%20%20%20private%20constructor()%3B%0D%0A%20%20%20%20%20%20%20%20readonly%20Creator%3A%20number%3B%0D%0A%20%20%20%20%20%20%20%20readonly%20Parent%3A%20any%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A)

Check first that the class has all the members of the Collection class.