That error says the Typescript compiler can't find the .tz(...)
method on moment
's type definitions (typings).
All you have to do is install moment-timezone
's typings, so it adds tz
to moment()
.
(You usually install typings for every lib you are going to use. The thing is that some, such as angular and moment, have their type definition files embedded in the libs' sources themselves, thus freeing you from the need to install their typings.)
So, as said, just install moment-timezone
's typings:
# if you have typings version 1.X.X
typings install moment-timezone --save --global
# if you have typings version 0.X.X
typings install moment-timezone --save --ambient
And everything should work... if you do one thing more:
Rename your configs from momentzone
to moment-timezone
(and add the .js
files):
var map = {
'moment': 'node_modules/moment/moment.js',
'moment-timezone': 'node_modules/moment-timezone/moment-timezone.js'
};
var packages = {
'moment': { defaultExtension: 'js' },
'moment-timezone': { defaultExtension: 'js' }
};
That is needed because the name you use here is the name you are going to use in the import
. And the name you use in the import
is the name the typescript compiler will use to find the type definitions. And the typings you installed define a module called moment-timezone
, not momentzone
.
After that, use:
import * as moment from 'moment';
import 'moment-timezone'; // since this module only has side-effects, this is enough
That should be all.
PS.: In the settings above, your compiler will pick moment
's typings from moment
's source AND moment-timezone
's typings (the tz
function) from the DefinitelyTyped repository (typings.json).
Sometimes, though, they don't play nice. If that happens to you, you'll have to override moment
's typings from source with moment
's typings from the DefinitelyTyped repository (typings.json).
In other words do:
# Only do this if installing moment-timezone alone didn't work...
# if you have typings version 1.X.X
typings install moment moment-timezone --save --global
# if you have typings version 0.X.X
typings install moment moment-timezone --save --ambient
import 'momentzone';
should suffice. – Bonhamtz
function in my console log but compiler doesn't – Hoedown