I am attempting to enhance the functionality of the moment.js library by adding a new function that requires a moment() call within its body. Unfortunately, I am struggling to achieve this.
Using the latest version of Typescript and moment.js, I have searched for a solution without success. Although I found a potential solution (Typescript: add function to momentjs' prototype) that seemed promising, it hasn't worked for me.
My current code snippet is as follows:
import * as moment from 'moment';
export namespace moment{
interface Moment{
myFunc(): boolean;
}
}
(moment as any).fn.myFunc = function() {
return moment(....);
};
I am not certain where the issue lies, as when attempting to use the moment library along with myFunc, importing moment (import * as moment from 'moment') does not seem to be enough – myFunc is not recognized, only the standard moment functions.
For example, when using myFunc(), it gives an error indicating that myFunc() is not recognized:
import * as moment from 'moment'
import Moment = moment.Moment
... moment().add(...) //works
... moment().myFunc() // doesn't recognize myFunc()
Any recommendations on how to resolve this issue and make it work?