How to use moment-duration-format in TypeScript? How to use moment-duration-format in TypeScript? typescript typescript

How to use moment-duration-format in TypeScript?


I was using the workaround here, but it now looks like the ype def has been fixed. Getting an update of the type def So I can do the following...

import * as moment from 'moment';import 'moment-duration-format';let duration = moment.duration(decimalHours, 'hours') ;   let options : moment.DurationFormatSettings   = {  forceLength : false,  precision : 0,  template : formatString,  trim : false};let result  = duration.format(formatString, 0, options);


You are using duration as a property instead of invoking it. Try:

let dd = moment.duration(400.99, 'hours').format('D:HH:mm');

This is actually not a TypeScript problem. It wouldn't work with JavaScript either. In JavaSctript you would get a runtime error while TypeScript doesn't let you do it at compile time (proving its worth).


I'm having the same problem...

It seems as if there is an error in the type definition. Others have same issue as you can see here: https://github.com/souldreamer/noti-cli/blob/ef104c22792e0dfeb67d3372b04e231d45ffaa55/src/shared/pipes.ts#L38

There was a fix for the problem as part of a pull request on github, that unfortunately has been closed without merging:https://github.com/DefinitelyTyped/DefinitelyTyped/pull/14327

As a temporary workaround, you could use the line of code of the first link. As second step we should try to create a new pull request on github, to get the problem fixed in future releases.