Maciej Sikora Maciej Sikora - 3 months ago 50
Javascript Question

How to extend js class from outside library in TypeScript / Using mojs in TypeScript

i know how to declare library ( example usage mojs):

declare var mojs:any;


but i have problem with extending from it:

class MyObject extends mojs.CustomShape {
getShape () {
return '..path'
}
};


i get error:


Type 'any' is not constructor object type


I tried something like this:

declare var mojs:Object{

CustomShape:any
};


but no luck - it is wrong code.

So how can I extend js library class in typescript?

Answer

Ok, i managed this by setting type as ObjectConstructor

My code for mojs:

declare var mojs: {
    CustomShape:ObjectConstructor, //here usage
    addShape:any,
    Burst:any,
    Timeline:any,
    Shape:any,
    ShapeSwirl:any
};

So with ObjectConstructor i can extend this class without errors like:

 class MyObject extends mojs.CustomShape {
   getShape () {
    return '..path'
   }
 };
Comments