typescriptstrong-typingdefinitelytypedtsd

What to do if a typings (or tsd) is not available?


I was looking over the TypeScript handbook and I can't seem to find the answer.

If I am using a library that no typings exist, what are my options?

One is to create the typings file, but this I don't really want to do.

What are my other options, I seem to remember some kind of 'declare' keyword ?

Or maybe something in the tsconfig ?

I assume there is a way of declaring this variable (type) globally so every time I use it, it would just work.

And I presume there is a way of just declaring it only available in one file.

Of course the best way would be to include a typings file but this won't always be available.


Solution

  • If your primary concern is just to get rid of the errors, you can simply write declare var MyUntypedLibrary: any; above your code, where MyUntypedLibrary is the name of the global reference to your dependency.

    If you need this reference in several files and don't want to repeat yourself, you could write it in the top of any file, above any namespaces, and it would be available to the whole project. If you have many untyped dependencies, it would probably be a good idea to have a separate ts-file where you define these.

    Note: This works fine when using local modules. I'd guess this might be more troublesome if one is using external modules and want to have one place to define an implicit dependency for them all, but then again each module should handle their own dependencies internally anyway.