TypeScript extend third-party library moment.js

I just want to add a function to the prototype interface Moment, which will always format the same every time I use it. I have already tried what can be found here.

declare module moment {
    export interface Moment {
        myFormat: () => string;
    }
}

And in another file implementation:

Moment.prototype.myFormat = ():string => { return this.format("DD.MM.YYY"); }

However, this does not work. I just want to be able to call moment(aDate).myFormat(), but I can't get it to work.

Already tried to use declare module "moment"some options moment.Moment, but still the same.

As indicated in the link, no declare. But then I get an error

'declare' required for a top-level item.

+4
source share
1 answer

It works

import moment from 'moment';
declare module 'moment' {
   export interface Moment {
     myFormat: () => string;
   }
 }
moment().myFormat(); 
0

Source: https://habr.com/ru/post/1692097/


All Articles