Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

typescript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...