Определение типа машинописного текста для продолжительности moment.js - PullRequest
0 голосов
/ 30 октября 2018

Я реализовал модуль, который берет две 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'.

Какая реализация исправит обе ошибки?

1 Ответ

0 голосов
/ 30 октября 2018

в зависимости от вашего импорта import * as moment from 'moment', тип для Длительность moment.Duration, а не moment.duration. Изменить это export default function roundDuration( startTime: moment.Moment, endTime: moment.Moment, timeRoundingRule: number ): moment.Moment.duration { ... }

в export default function roundDuration( startTime: moment.Moment, endTime: moment.Moment, timeRoundingRule: number ): moment.Duration{ ... }

...