How do I extend a TypeScript class definition in a separate definition file?

后端 未结 5 1812
执笔经年
执笔经年 2020-12-08 04:09

I have a JS library called leaflet which has an existing TypeScript definition file.

I wish to use a plugin which extends some of the objects in leaflet with an extr

5条回答
  •  春和景丽
    2020-12-08 04:26

    This is what I tried and it feels relatively comfortable for me

    declare module L {
        export class CircleMarkerEx {
            constructor(source: CircleMarker);
            public bindLabel(name: string, options: any): CircleMarker;
        }
        export function ex(cm: CircleMarker): CircleMarkerEx;
    }
    

    where CircleMarkerEx and ex can be defined as

    class CircleMarkerExtender extends CircleMarker {    
        public b() {
            return `${this.a()} extended`;
        }
    }
    CircleMarker.prototype = Object.create(CircleMarkerExtender.prototype);
    CircleMarker.prototype.constructor = CircleMarker;
    
    export function ex(cm: CircleMarker) {
        return cm as CircleMarkerExtender;
    }
    

    then

    let cm = ex(circle).bindLabel("name", {});
    

    but it is still a little strange

提交回复
热议问题