【发布时间】:2018-10-30 06:38:25
【问题描述】:
我已经实现了一个模块,它采用两个 moment.js 时间戳和一个舍入规则并返回这些时间戳之间的舍入 moment.js 持续时间。
我的第一反应是使用moment.Moment 来指定预期的输出类型,如下所示。
export default function roundDuration(
startTime: moment.Moment,
endTime: moment.Moment,
timeRoundingRule: number
): moment.Moment {
const duration = moment.duration(endTime.diff(startTime));
// calculate rounded duration based on rounding rule
return roundedDuration;
}
但持续时间与时刻不同。
在我的规范文件中,
import * as moment from 'moment';
import roundDuration from './rounding';
test('roundDuration rounds zero seconds to zero minutes duration using rule UpToNearestMinute', () => {
const startTime = moment('05-17-2018 23:40:00', 'MM-DD-YYYY hh:mm:ss');
const endTime = moment('05-17-2018 23:40:00', 'MM-DD-YYYY hh:mm:ss');
const timeRoundingRule = 0;
const roundedTime = roundDuration(startTime, endTime, timeRoundingRule);
expect(roundedTime.asMinutes()).toBe(0);
});
我收到以下 linting 错误:
[ts] Property 'asHours' does not exist on type 'Moment'. Did you mean 'hours'?
寻找可以消除此错误的特定类型定义,例如moment.Moment.duration 或moment.duration:
export default function roundDuration(
startTime: moment.Moment,
endTime: moment.Moment,
timeRoundingRule: number
): moment.Moment.duration { ... }
产量
[ts] Namespace 'moment' has no exported member 'Moment'.
或
[ts] Namespace 'moment' has no exported member 'duration'.
哪个实现可以纠正这两个错误?
【问题讨论】:
标签: typescript types momentjs