moment-duration-format.d.ts Definition Not Extending Moment Module
Asked Answered
B

4

9

Any idea why this doesn’t work or how I can 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');

I’m getting the error that ‘format' does not exist on type ‘Duration'

Bruin answered 29/12, 2016 at 16:16 Comment(0)
H
12

First, install types:

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

Second, import them in 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');
Healy answered 9/5, 2017 at 10:53 Comment(1)
npm install --save-dev @types/moment-duration-format solved my problem, while npm install moment-duration-format didn't.Pappose
S
11

Imports:

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 need to export and import it as well.

Shinar answered 31/12, 2016 at 22:16 Comment(3)
I'm still getting errors in my code. Property 'duration' does not exist on type 'typeof 'moment''.Bruin
I'd need to have a look at your code to better understand where your error comes from. But do have a look at my answer, I updated the interface definitions. I don't think it will solve your problem, but once fixed, it will provide better autocomplete.Shinar
Works like a charm! ThanksPung
L
1

You need to have the following dependencies installed:

"dependencies": {
  "@types/moment-duration-format": "2.2.2",
  "moment": "2.24.0",
  "moment-duration-format": "2.3.2"
}

If that's the case then you need these imports in the exact same order:

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

Afterwards you should be able to do this:

const seconds: number = Math.floor(process.uptime());
const formatted: string = moment.duration(seconds, 'seconds').format({
  precision: 0,
  template: 'y [years], w [weeks], d [days], h [hours], m [minutes], s [seconds]',
});
Liman answered 16/1, 2020 at 10:39 Comment(0)
T
0

you should use namespace instead of module

declare namespace moment {
     interface Duration {
       format(template: string, precision?: string, settings?: DurationSettings): string;
     }

}

and settings for "moment-duration-format": "^2.3.2"

        interface DurationSettings {
                trim: boolean | 'left' | 'right';
                useGrouping: boolean;
                usePlural: boolean;
                useLeftUnits: boolean;
                precision: number;
                useSignificantDigits: boolean;
                trunc: boolean;
                forceLength: boolean;
                minValue: number;
                maxValue: number;
                largest: number;
        }
Trapeze answered 3/1, 2023 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.