moment-duration-format.d.ts定义不扩展矩量模块 [英] moment-duration-format.d.ts Definition Not Extending Moment Module

查看:143
本文介绍了moment-duration-format.d.ts定义不扩展矩量模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道为什么这不起作用或如何扩展持续时间界面以支持格式化功能?

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

}

用作:

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

我收到'持续时间'类型'格式'不存在的错误

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

推荐答案

进口商品:

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

然后在你的代码中:

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.

这篇关于moment-duration-format.d.ts定义不扩展矩量模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆