最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript: Add a function to moment.js namespace - Stack Overflow

programmeradmin1浏览0评论

I'm trying to add extra functionality to moment.js library. I want to add a function that itself requires a moment() call in its body and I'm having a hard time figuring this out.

I'm using the latest version of Typescript and moment.js. I've tried to find a solution but I've e up with nothing. This solution (Typescript: add function to momentjs' prototype) has e close to working, I think, but still nothing.

So far what I have is:

import * as moment from 'moment';

export namespace moment{
    interface Moment{
        myFunc(): boolean;
    }
}

(moment as any).fn.myFunc = function() {
    return moment(....);
};

I'm not sure where I'm going wrong but when I try to use the moment library and myFunc I thought importing moment (import * as moment from 'moment') would be enough but myFunc isn't recognized, only the standard moment functions.

Ex. This says myFunc() isn't recognized.

import * as moment from 'moment'
import Moment = moment.Moment

... moment().add(...) //works
... moment().myFunc() // doesn't recognize myFunc()

Any suggestions on how to get this to work?

I'm trying to add extra functionality to moment.js library. I want to add a function that itself requires a moment() call in its body and I'm having a hard time figuring this out.

I'm using the latest version of Typescript and moment.js. I've tried to find a solution but I've e up with nothing. This solution (Typescript: add function to momentjs' prototype) has e close to working, I think, but still nothing.

So far what I have is:

import * as moment from 'moment';

export namespace moment{
    interface Moment{
        myFunc(): boolean;
    }
}

(moment as any).fn.myFunc = function() {
    return moment(....);
};

I'm not sure where I'm going wrong but when I try to use the moment library and myFunc I thought importing moment (import * as moment from 'moment') would be enough but myFunc isn't recognized, only the standard moment functions.

Ex. This says myFunc() isn't recognized.

import * as moment from 'moment'
import Moment = moment.Moment

... moment().add(...) //works
... moment().myFunc() // doesn't recognize myFunc()

Any suggestions on how to get this to work?

Share Improve this question edited Aug 4, 2017 at 20:00 J. Doe asked Aug 4, 2017 at 19:59 J. DoeJ. Doe 731 silver badge4 bronze badges 3
  • because myFunc does not exists in moment – Aravind Commented Aug 4, 2017 at 20:00
  • 1 You should import your own moment type, not the one exported by moment. So change import * as moment from 'moment' to import * as moment from './moment-extended' or something. – csander Commented Aug 4, 2017 at 20:17
  • Okay so I changed my code to how @csander suggested but it's not working. Am I doing this right? I import * as moment from 'moment' and * as myMoment from './moment.extended in the example above and I try to call myFunc like this: 'myMoment.myFunc()' but I get this error: Property 'myFunc' does not exist on type 'typeof "c:/Git/.../moment.extended"' – J. Doe Commented Aug 4, 2017 at 21:04
Add a ment  | 

1 Answer 1

Reset to default 9

You can extend moment to include your myFunc using TypeScript's declaration merging.

The following works for me (using TypeScript 2.4.2):

import * as moment from 'moment';

declare module "moment" {
  interface Moment {
    myFunc(): moment.Moment;
  }
}

(moment as any).fn.myFunc = function (): moment.Moment {
  console.log("Called myFunc!");
  return moment();
};

console.log(moment().myFunc().valueOf());

And outputs:

Called myFunc!
1501901308611
发布评论

评论列表(0)

  1. 暂无评论