简体   繁体   中英

correct ES6 import for adding moment precise range plugin

I'm using typescript within an angular 4 project and want to use moment-precise-range-plugin.

I've installed moment following https://momentjs.com/docs/#/use-it/typescript/ but i want to add the preciserange plugin http://momentjs.com/docs/#/plugins/preciserange/ .

Install for Node.js for the preciserange plugin is at https://codebox.org.uk/pages/moment-date-range-plugin and specifies require('moment-precise-range-plugin'); but there is no Typescript install.

I've tried import { Moment } from 'moment-precise-range-plugin'; but get the following typescript error

Could not find a declaration file for module 'moment-precise-range-plugin'. 'c:/Users/[UserName]/app/node_modules/moment-precise-range-plugin/moment-precise-range.js' implicitly has an 'any' type. Try npm install @types/moment-precise-range-plugin if it exists or add a new declaration (.d.ts) file containing declare module 'moment-precise-range-plugin';'

Any ideas very much welcome!

I'm using the moment-precise-range-plugin in an Angular 4 project and was able to import it with:

import 'moment-precise-range-plugin';

I imported it after importing moment: import * as moment from 'moment';

One thing to note: my TS linter still warns me that preciseDiff() will cause problems, but it all runs just fine. Try that out and see if it works.

NOTE: I'm using Angular 10.

It works with me when import it as -> import 'moment-precise-range-plugin';

and add these 3 lines:

declare module 'moment' {
  function preciseDiff(start: string | Date | moment.Moment, end: string | Date | moment.Moment, convertToObject?: boolean): any;
}

I fixed it by adding a definition file in src/typings.d.ts

import * as moment from 'moment';

export = moment;

declare module 'moment' {
  interface Moment {
    preciseDiff(start: string | Date | moment.Moment): string;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM