Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Augment third party library Typescript

I'm developing an Angular 5 project. I have referenced a third party library downloaded from NPM (odata-v4-ng). I need to augment a class inside this library to add some methods. The class is declared like this.

export declare class ODataQuery extends ODataQueryAbstract {
  // methods omitted for brevity
}

I have tried to augment the class this way:

import { ODataQuery } from 'odata-v4-ng';

declare module 'odata-v4-ng' {
  interface ODataQuery {
    test();
  }
}

ODataQuery.prototype.test = () => { };

The problem is that compilers throws saying "'ODataQuery' only refers to a type, but is being used as a value here.". If I create a class in my Angular project and augment it this way, everything works fine.

Any idea?

like image 805
user1350775 Avatar asked Jul 10 '26 00:07

user1350775


1 Answers

That specific class is defined in another module and is only reexported by the main odata-v4-ng module. If you extends the actual module where the class is defined everything seems to work fine:

import { ODataQuery } from 'odata-v4-ng';

declare module './node_modules/odata-v4-ng/src/app/odata/odata-query/odata-query' {
  export interface ODataQuery {
    test():void
  }
}

ODataQuery.prototype.test = () => { };
like image 96
Titian Cernicova-Dragomir Avatar answered Jul 11 '26 20:07

Titian Cernicova-Dragomir