Typescript 矩.js持续时间的类型脚本类型定义

Typescript 矩.js持续时间的类型脚本类型定义,typescript,types,momentjs,Typescript,Types,Momentjs,我实现了一个模块,它接受两个moment.js时间戳和一个取整规则,并返回这些时间戳之间取整的moment.js持续时间 我的第一反应是使用moment.moment指定预期的输出类型,如下所示 export default function roundDuration( startTime: moment.Moment, endTime: moment.Moment, timeRoundingRule: number ): moment.Moment { const durat

我实现了一个模块,它接受两个
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]类型“矩”上不存在属性“asHours”。你是说“小时”吗?

正在查找可消除此错误的特定类型定义,如
moment.moment.duration
moment.duration

export default function roundDuration(
    startTime: moment.Moment,
    endTime: moment.Moment,
    timeRoundingRule: number
): moment.Moment.duration { ... }
屈服

[ts]命名空间“矩”没有导出的成员“矩”。

[ts]命名空间“时刻”没有导出的成员“持续时间”。


哪种实现可以纠正这两个错误?

基于您的导入是从“时刻”开始的
import*as时刻
,持续时间的类型是
moment.Duration
,而不是
moment.Duration
。改变这个

导出默认函数roundDuration(
开始时间:时刻,时刻,
结束时间:时刻,时刻,
timeRoundingRule:数字
):moment.moment.duration{…}

导出默认函数roundDuration(
开始时间:时刻,时刻,
结束时间:时刻,时刻,
timeRoundingRule:数字
):时刻。持续时间{…}