最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use moment-duration-format in TypeScript? - Stack Overflow

programmeradmin1浏览0评论

I am using moment.js in my TypeScript (Ionic2/Angular2) project. Following on from this post, I now want to try out a plugin moment-duration-format

I have the npm package and the type definition and am able to use by import as..

import moment from 'moment';
...
let duration = moment.duration(decimalHours, 'hours');

I now want to use moment-duration-format

I have installed via npm install moment-duration-format --save and then the type definition via npm i @types/moment-duration-format --save.

I can see both npm modules.

As always, there is always some mystery on how to use such type definitions (the useage including import never seems to be in any doco).

I have tried adding import 'moment-duration-format';, import duration from 'moment-duration-format'; (moment-duration-format/index.d.ts' is not a module.)

I get an error when trying to use as follows..

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

// (TS error [ts] Property 'format' does not exist on type '(inp?: DurationInputArg1, unit?: DurationConstructor) => Duration'.

Does anyone have any idea on how to use this in TypeScript.

Thanks in advance

I am using moment.js in my TypeScript (Ionic2/Angular2) project. Following on from this post, I now want to try out a plugin moment-duration-format

I have the npm package and the type definition and am able to use by import as..

import moment from 'moment';
...
let duration = moment.duration(decimalHours, 'hours');

I now want to use moment-duration-format

I have installed via npm install moment-duration-format --save and then the type definition via npm i @types/moment-duration-format --save.

I can see both npm modules.

As always, there is always some mystery on how to use such type definitions (the useage including import never seems to be in any doco).

I have tried adding import 'moment-duration-format';, import duration from 'moment-duration-format'; (moment-duration-format/index.d.ts' is not a module.)

I get an error when trying to use as follows..

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

// (TS error [ts] Property 'format' does not exist on type '(inp?: DurationInputArg1, unit?: DurationConstructor) => Duration'.

Does anyone have any idea on how to use this in TypeScript.

Thanks in advance

Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Mar 28, 2017 at 0:48 petercpeterc 7,84313 gold badges79 silver badges161 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 18

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.

The problem is as R_B_ correctly worked out a wrong type definition in the typings module for moment-duration-format.

I'll create a new pull request for this, as my last was closed because of lint errors.

So long you can use a custom type definition in your project as done here https://github.com/TwoStone/leaderboard/blob/develop/subprojects/webapp/src/custom-typings.d.ts Just create the file in your project and it should work for the moment.

发布评论

评论列表(0)

  1. 暂无评论