snippettypescriptangularCritical
How to use moment.js library in angular 2 typescript app?
Viewed 0 times
angularlibraryusetypescriptmomentapphow
Problem
I tried to use it with typescript bindings:
test.ts:
And without:
test.ts:
But when I call moment.format(), I get an error.
Should be simple, can anybody provide a command line/import combination that would work?
npm install moment --save
typings install moment --ambient -- savetest.ts:
import {moment} from 'moment/moment';And without:
npm install moment --savetest.ts:
var moment = require('moment/moment');But when I call moment.format(), I get an error.
Should be simple, can anybody provide a command line/import combination that would work?
Solution
Update April 2017:
As of version 2.13.0, Moment includes a typescript definition file. https://momentjs.com/docs/#/use-it/typescript/
Just install it with npm, in your console type
And then in your Angular app, import is as easy as this:
That's it, you get full Typescript support!
Bonus edit: To type a variable or property as
As of version 2.13.0, Moment includes a typescript definition file. https://momentjs.com/docs/#/use-it/typescript/
Just install it with npm, in your console type
npm install --save momentAnd then in your Angular app, import is as easy as this:
import * as moment from 'moment';That's it, you get full Typescript support!
Bonus edit: To type a variable or property as
Moment in Typescript you can do this e.g.:let myMoment: moment.Moment = moment("someDate");Code Snippets
npm install --save momentimport * as moment from 'moment';let myMoment: moment.Moment = moment("someDate");Context
Stack Overflow Q#35166168, score: 593
Revisions (0)
No revisions yet.