Moment-duration-format.d.ts Definition Non-Expansion Momentum Module

Any idea why this is not working or how can I extend the duration interface to support the format function?

declare module 'moment' { interface Duration { format(template: string, precision?: string, settings?: any): string; } } 

when used as:

 moment.duration(minutes, 'minutes').format('mm'); 

Im getting an error that 'format' does not exist in type 'Duration'

+6
source share
2 answers

Import

 import * as moment from 'moment'; import 'moment-duration-format'; 

Outside of your class, define the interfaces:

 interface Duration extends moment.Duration { format: (template?: string, precision?: number, settings?: DurationSettings) => string; } interface DurationSettings { forceLength: boolean; precision: number; template: string; trim: boolean | 'left' | 'right'; } 

Then in your code:

 const duration = moment.duration(minutes, 'minutes') as Duration; return duration.format('mm'); 

If you defined your Duration interface in another file, you will also need to export and import it.

+5
source

First set the types:

 npm install --save-dev @types/moment-duration-format 

Secondly, import them into your file:

 /// <reference path='../..your-path.../node_modules/@types/moment-duration-format/index.d.ts' /> import * as moment from 'moment'; import 'moment-duration-format'; 

Then you can use

 moment.duration(minutes, 'minutes').format('mm'); 
+2
source

Source: https://habr.com/ru/post/1013608/


All Articles