I'm having a weird issue with Typescript and importing the moment package. I've seen it in a couple of different places, and I've even seen it crop up in the same file depending on whether or not I added a specific static method to a class.
The problem is this:
When I import moment with import * as moment from 'moment';, I get no Typescript errors, but I do get an issue with my tests; specifically, they fail with TypeError: moment is not a function.
When I change the import the import moment from 'moment';, the code works, but I get "moment" has no default export.
I don't understand the difference between these. I've seen other questions (like this one, which recommends the first syntax). The a comment on the accepted answer to the question above suggests turning on the allowSyntheticDefaultImports flag, which does correct the issue, but I'm a little concerned about that flag. The documentation says:
Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
which seems to imply that there is no typechecking for modules imported in this way.
So, two questions:
- Am I understanding the TS documentation for the
allowSyntheticDefaultImportsflag correctly? Is it turning off typechecking for themomentpackage? - Is there a way to (consistently) import this package?
FWIW, I'm using Typescript 3.4.5 and moment 2.24.0.