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

后端 未结 3 805
无人及你
无人及你 2021-01-02 15:02

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         


        
3条回答
  •  猫巷女王i
    2021-01-02 15:10

    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.

提交回复
热议问题