知道为什么这不起作用或如何扩展持续时间接口以支持格式功能?
declare module 'moment' { interface Duration { format(template: string,precision?: string,settings?: any): string; } }
当用作:
moment.duration(minutes,'minutes').format('mm');
我收到“持续时间”类型中“格式”不存在的错误
解决方法@H_403_12@
进口:
import * as moment from 'moment';
import 'moment-duration-format';
在您的课程之外,定义接口:
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');
如果您在另一个文件中定义了Duration接口,则还需要导出和导入它.
import * as moment from 'moment'; import 'moment-duration-format';
在您的课程之外,定义接口:
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');
如果您在另一个文件中定义了Duration接口,则还需要导出和导入它.