As of v0.5.29 importing via Typescript seems to be broken:
// worked until v0.5.28
import moment from 'moment-timezone';
yields:
TS2349: This expression is not callable.
Type 'typeof import("node_modules/moment/moment.d.ts")' has no call signatures.
It is due to the addition of an index.d.ts file which was not part the releases prior to v0.5.29
As a workaround I installed @types/moment-timezone and re-routed the type definition to it in tsconfig:
"compilerOptions": {
"paths": {
"moment-timezone": ["node_modules/@types/moment-timezone/index.d.ts"]
},
...
}
Just make sure that the compiler option baseUrl is also defined
Fixed in 0.5.30, please pull that
I experienced this issue with moment-timezone 0.5.31 and @types/moment-timezone 0.5.13.
Uninstalling @types/moment-timezone fixed the issue.
Just saw our builds breaking because we have @types/moment-timezone as a dependency:
.../node_modules/@types/moment-timezone/moment-timezone.d.ts(66,9): error TS2451: Cannot redeclare block-scoped variable 'tz'.
.../node_modules/moment-timezone/index.d.ts(63,11): error TS2451: Cannot redeclare block-scoped variable 'tz'.
All I had to do was uninstall @types/moment-timezone. It's definitely nice to have official types packaged with a library, but I think this error of mine has the potential to affect everyone using Typescript with moment-timezone. It might be good to do what moment did and replace @types/moment-timezone with a stub and deprecation notice.
Base on that answer their is an issue with the way the index.d.ts is done:
https://stackoverflow.com/a/48846446/1427338
i temporarily put a // @ts-ignore in the index.d.ts, i could not suggest a better fix.
@ellenaua thanks a lot, your PR seemed to solve it.
@mattcasey that's so bad - I am not sure what's better to do now:
UPD: I see, we should mark @types/moment-timezone deprecated
@ellenaua I tend to agree with you because i continue to accidentally install @types/moment and have seen others do the same! We maintain about 20 repos that use moment-timezone and if the fix is simply "uninstall @types/moment-timezone" then that's not too hard to do. FWIW, based on the error I got, it was pretty obvious that types had been added to this lib and I just came here immediately to see what's up and confirm.
A warning on the web page would be the fastest solution for most people, I think.
Most helpful comment
I experienced this issue with moment-timezone 0.5.31 and @types/moment-timezone 0.5.13.
Uninstalling @types/moment-timezone fixed the issue.