-1

Unable to use the Javascript plugin 'moment-weekday-calc' in a Node.Js Typescript project.
moment-weekday-calc does not have a typed version.

My code as written in Javascript:

const moment = require('moment');
require('moment-weekday-calc');

const result = moment().dateRangeToDates({
      rangeStart,
      rangeEnd,
      exclusions,
      weekdays: [1, 2, 3, 4, 5]
});   

Potential Typescript code:

import * as moment from 'moment';
import 'moment-weekday-calc';

const result = moment().dateRangeToDates({
      rangeStart,
      rangeEnd,
      exclusions,
      weekdays: [1, 2, 3, 4, 5],
})

Error: Property 'dateRangeToDates' does not exist on type 'Moment'.ts(2339)

I've tried something like declare module 'moment-weekday-calc' but still no luck and I think moment-weekday-calc is unable to add the new modules to moment.

Thanking you in anticipation.

Ezekiel
  • 167
  • 3
  • 11

1 Answers1

1

To just make it works, you can extend moment to include 'moment-weekday-calc' functions using TypeScript's declaration merging. But you have to define each functions yourself. And then just use require('moment-weekday-calc') in where you want to use those functions.

To use isoWeekdayCalc/weekdayCalc from 'moment-weekday-calc':

import moment from 'moment';

declare module "moment" {
    interface Moment {
        isoWeekdayCalc(d1: Date | string, d2: Date | string, weekDays: number[], exclusions?: string[], inclusion?: string[]): number
        weekdayCalc(d1: Date | string, d2: Date | string, weekDays: string[] | number[], exclusions?: string[], inclusion?: string[]): number
    }
}

Another work around, just simply add //@ts-ignore before your call the functions

//@ts-ignore
moment().weekdayCalc('1 Apr 2015','31 Mar 2016',[1,2,3,4,5],['6 Apr 2015','7 Apr 2015'],['10 Apr 2015']); //260 
mcjai
  • 558
  • 4
  • 9