ruby-on-railsangulardatetimemomentjsmoment-timezone

How to fix error TS2339: Property 'tz' does not exist on type 'Moment'?


In rails app, I created frontend app which uses angular. Here I am using moment and moment-timezone like

"moment": "^2.22.2",
"moment-timezone": "^0.5.23",

In timezone.service.ts importing and using like

import * as moment from 'moment-timezone';
import {Moment} from 'moment';

public parseDatetime(datetime:string, format?:string):Moment {
    var d = moment.utc(datetime, format);

    if (this.ConfigurationService.isTimezoneSet()) {
      d.local();
      d.tz(this.ConfigurationService.timezone());
    }

    return d;
  }

While running bundle exec rake assets:precompile getting ERROR in src/app/components/datetime/timezone.service.ts(55,9):

error TS2339: Property 'tz' does not exist on type 'Moment'. src/app/components/datetime/timezone.service.ts(80,23): error TS2339: Property 'tz' does not exist on type 'typeof moment'.

npm version is 6.4.1

node version is v8.12.0

ruby verson is ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux]

Please help me to solve this issue.


Solution

  • I think you need to update your version of moment-timezone. There is a documented bug on 5.28, but since you're using a lower version in might exist there as well.

    https://github.com/moment/moment-timezone/issues/906

    this states that 5.33 and 5.31 are without error and in a rails app, using moment-timezone 5.40 I am able to us the tz function without error.

    ds.tz("America/Chicago")
    

    From my working package.json

    "moment": "^2.29.4",
    "moment-timezone": "^0.5.40",