I've started to migrate a large existing, namespace
and <reference>
based TypeScript application to use modules
and the @types
NPM repository. However, I'm running into problems in every second.
- Lots of libraries don't even have typings there.
- Lots of libraries have completely outdated typings there.
- Lots of libraries have a
global
typings file, resulting that I cannotimport
it because TS complains that it's not a module. - When a library has a
module
shaped typing file correctly included in it's NPM package (likemoment.js
), TS still seems to be unable to find it, and errors the lineimport * as moment from 'moment'
saying module is not found.
What is the current state of this technique? As I see it at first glance, it seems to be far from being production ready.
What are the hacks and techniques to overcome these issues?
I'm targetting es5
and using es6
as modules
configuration. Can this be the problem? As far as I know TS supports the es6
module syntax for es5
output as well.
The current way things are done is definitely production-ready; however, there's a few things which aren't obvious and that isn't your fault.
Let me try to answer your questions one-by-one.
In TypeScript 2.1, as long as you have a package installed in
node_modules
and you don't havenoImplicitAny
turned on, you can just import whatever you want.If you do want to use
noImplicitAny
(which I would recommend for any project that you expect to grow over time), you can just always create adeclarations.d.ts
in your project source folder that declares the modules as you need:You should definitely feel free to send a pull request to DefinitelyTyped, but if you're really strapped for time, you can just use the approach I gave for point (1).
If you need to use a global declaration file from DefinitelyTyped, and it's not automatically included in your project, you may have to add it to the
types
field in yourtsconfig.json
. See my other answer here, but the crux of it is that if you need to include global declarations for thefoo
andbar
packages, and you have@types/foo
and@types/bar
installed, you can write the following in yourtsconfig.json
.I believe that this has to do with the fact that you're targetting ES6. Try changing your
"moduleResolution"
option to"node"
in yourtsconfig.json
.Sorry that that is a gotcha. The rationale is that the only module loading system that actually has this resolution semantics is CommonJS (the strategy that Node uses). I agree that that could be improved.