I have installed moment and its typings into my ionic2 project:
typings install moment --ambient --save
In my page, I use the following to import syntax:
import * as moment from 'moment';
With this import syntax (as opposed to others) the moment functionality is working. However, Visual Code intellisense says it “cannot find ‘moment’” and when doing an ionic serve get the following errors:
ERROR in [default] C:/Users/Mark/Documents/App Development/MyApp/app/pages/
login/login.ts:4:24
Cannot find module 'moment'.
and
ERROR in [default] typings/browser/ambient/moment/index.d.ts:9:20
Cannot find namespace 'moment'.
If anyone has been able to get momentJS actually working (not just notionally following the procedures of other 3rd party package installs) in their TS ionic2 project without any intellisense or other associated errors can you please provide all the steps required (including their TSConfig.json set-up)?
Thanks! Removing my ‘ambient’ typings installation of moment and doing the install as you suggested fixed the intellisense and the TS errors I was getting during ionic serve.
where this.my.birthdateYear is a string value input from the user, which worked in ionic1 JS (except was using $scope instead of this). Now get the following error after serve:
Argument of type 'string' is not assignable to parameter of type 'Moment'.
I don’t see any errors with similar code. You seem to have an extra closing parenthesis at the end of that expression, so is there any chance that there’s some sort of unintended weird parenthesis grouping that’s causing your problem?
Apologies - I cut that example out of conditional code and accidentally kept the ending parenthesis. In any case that was not the moment call that was causing the error! It was
moment().diff(birthDate, 'years')
where birthdate is a string. As per the momentJS docs, a string value is permitted for the first parameter, and is what I had working in normal javascript. It looks like the Type Definition for the diff method is not complete/correct.
Thus in accordance with the error I was receiving I needed to do this:
let birthDateMoment=moment(birthDate);
let Age = moment().diff(birthDateMoment, 'years');
So all is working now.
In summary - when installing momentJS or some other 3rd party packages, using an ambient typings install as suggested in places such as
is not always the appropriate type of install to perform depending on the package. And don’t always trust that the Typescript Definitions are complete/correct!