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

后端 未结 3 806
无人及你
无人及你 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条回答
  •  佛祖请我去吃肉
    2021-01-02 15:16

    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]',
    });
    

提交回复
热议问题