Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'format' does not exist on type 'Duration'

I'm trying to use "moment-duration-formation" in TypeScript but, even though it works, webpack keeps on complaining that it can't find the "format" method on the line:

return moment.duration(value, "minutes").format("...");

Here is the package.json

{
  "devDependencies": {
    "@types/moment-duration-format": "^1.3.7"
  },
  "dependencies": {
    "moment": "^2.18.1",
    "moment-duration-format": "^1.3.0"
  }
}

And the tsconfig.json:

{
  "compilerOptions": {
    "typeRoots": [ "./node_modules/@types" ]
  }
}

In the (angular2) component:

import * as moment from "moment";
import * as momentDurationFormat from "moment-duration-format";

...

let result: string = moment.duration(this.selectedHangarDistance * 10, "minutes").format("...")

I also tried with

import "moment-duration-format";

But it does not change anything:

ERROR in [at-loader] ./src/app/components/building/shop/shop.component.ts:190:81 TS2339: Property 'format' does not exist on type 'Duration'.

like image 665
ssougnez Avatar asked Nov 19 '22 06:11

ssougnez


1 Answers

A solution that worked for me:

import * as moment from "moment";
import momentDurationFormatSetup from "moment-duration-format";
momentDurationFormatSetup(moment);

The * as moment was significant as import moment from "moment" was causing type issues when being passed momentDurationFormatSetup.

like image 95
Richard Fernandez Avatar answered Dec 23 '22 12:12

Richard Fernandez