如果打字(或 tsd)不可用怎么办? [英] What to do if a typings (or tsd) is not available?

查看:19
本文介绍了如果打字(或 tsd)不可用怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 TypeScript 手册,但似乎找不到答案.

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?

一个是创建typings文件,但我真的不想这样做.

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 ?

或者可能是 tsconfig 中的某些内容?

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.

当然最好的方法是包含一个typings文件,但这并不总是可用.

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

推荐答案

如果您主要关心的是摆脱错误,您可以简单地在代码上方编写 declare var MyUntypedLibrary: any;,其中 MyUntypedLibrary 是对您的依赖项的全局引用的名称.

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.

如果您在多个文件中需要此引用并且不想重复自己,您可以将它写在任何文件的顶部,任何命名空间的上方,并且它可供整个项目使用.如果您有许多无类型依赖项,最好有一个单独的 ts 文件来定义这些依赖项.

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.

这篇关于如果打字(或 tsd)不可用怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆