admin管理员组

文章数量:1404950

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

I've installed moment following / but i want to add the preciserange plugin /.

Install for Node.js for the preciserange plugin is at 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 wele!

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

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

Install for Node.js for the preciserange plugin is at https://codebox.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 wele!

Share edited Aug 3, 2017 at 9:12 fidev asked Jul 28, 2017 at 17:35 fidevfidev 1,2523 gold badges24 silver badges53 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

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;
  }
}

本文标签: javascriptcorrect ES6 import for adding moment precise range pluginStack Overflow