简体   繁体   English

如何强制键入将全局(环境)模块用作外部模块?

[英]How to force typings to use a global (ambient) module as external one?

I want to use some .d.ts definition in my project, let's say, jQuery. 我想在我的项目中使用一些.d.ts定义,比方说jQuery。 I want to include it like this (or in a similar way) 我想这样包含它(或以类似方式)

import {$} from 'jquery';

and not have it in global scope (there are numerous name conflicts in my actual use cases). 而不是在全球范围内使用(我的实际用例中有很多名称冲突)。

But dt~jquery is a global module. 但是dt〜jquery是一个全局模块。 Obviously, I get an error: 显然,我得到一个错误:

> typings install dt~jquery --save

typings ERR! message Attempted to compile "jquery" as an external module,
  but it looks like a global module. You'll need to enable the global option to continue.

Can't typings automatically wrap it in declare module (why not)? typings不能自动将其包装在declare module (为什么不这样)? If not, what is the conventional way of doing it manually so that my repository contributors won't need to worry about it? 如果不是,那么手动执行此操作的常规方式是什么,以使我的存储库贡献者不必担心它?

I suppose a local modified .d.ts copy is needed. 我想需要一个本地修改的.d.ts副本。 If so, where should it be placed (my typings directory is .gitignored, so custom .d.ts should probably be stored separately and refrenced from typings.json)? 如果是这样,应该将其放置在哪里(我的typings目录是.gitignored,因此自定义.d.ts可能应该单独存储,并不能与types.json相对)?

UPDATE: I have just managed to convert jQuery and other global modules to external ones by simply wrapping each in declare module 'module_name' { ... } , placing under a folder I named custom_typings and creating index.d.ts with refrences there. 更新:我已经设法将jQuery和其他全局模块转换为外部模块,只需将它们包装在declare module 'module_name' { ... } ,放在我命名为custom_typings的文件夹下,并在其中创建index.d.ts It works fine with my IDE and compiles. 它可以在我的IDE上正常工作并进行编译。 The problem is, tsc does not include those imports in my bundled amd output. 问题是, tsc在捆绑的amd输出中不包括那些导入。 This is strange, because I had the exactly opposite issue here (tsc included external .d.ts import in js output, which confused webpack). 这很奇怪,因为我在这里遇到了完全相反的问题(tsc在js输出中包括了外部.d.ts导入,这使webpack感到困惑)。

// modified_jquery_typing.d.ts
declare module 'jquery' {
  ...code from DefinitelyTyped...

  export var jQuery: JQueryStatic;
}

// app.ts
import {thing} from 'actual_ts_file';
import {jQuery} from 'jquery';

// app.js contains .ts import but not .d.ts
define("app", ["require", "exports", "actual_ts_file"], ...)

UPDATE(2) : This was stupid, the jQuery import was simply left out because it was unused. UPDATE(2) :这很愚蠢,只是由于未使用jQuery导入而将其忽略了。 It actually works. 它确实有效。 Still, I would like to know whether this is the right way to do what I want. 不过,我想知道这是否是执行我想要的正确方法。

I think you only need import $ from 'jquery'; 我想你只需import $ from 'jquery'; (no braces) because the export IS what you want, rather than being a module of multiple exports from which you are extracting only one. (无括号),因为导出是您想要的,而不是作为多个导出的模块,而您仅从中提取一个。

Then, are you planning on using both versions of jquery simultaneously? 然后,您打算同时使用两个版本的jquery吗? The typings don't need to deal with code that you're not asking the typescript compiler to consider. 键入不需要处理您不要求打字稿编译器考虑的代码。 If you have some no-conflict version of $ being passed in to your typescript code then as usual you'll have to pass around that non-global reference. 如果您将$某些版本无冲突的$传递给您的打字稿代码,那么通常您将必须传递该非全局引用。

That import should also bring in the type definitions you need to strongly type the JQuery typed parameter that you'll be passing around. 该导入还应引入您需要强类型键入将要传递的JQuery类型参数的类型定义。

=== ===

While I absolutely sympathize with special-circumstance typings and import with typescript confusion, I'm not following your exact desired setup. 虽然我完全同意特殊情况下的输入并因打字稿混淆而导入,但我没有遵循您所需要的确切设置。 Type definitions are not private so as far as typescript is concerned, the types will be available everywhere. 类型定义不是私有的,就打字稿而言,类型随处可见。 That's not the same as polluting the run-time global namespace, which a d.ts file WON'T do. 这与污染运行时全局名称空间不同,d.ts文件不会这样做。 The types will be available everywhere to constrain your code, but it doesn't add any overhead to the run-time. 这些类型随处可见以约束您的代码,但不会增加运行时的开销。

The risk is having an ambient definition for the $ name whereas at runtime, it won't really be there - defeating the purpose of the strongly-typed environment in that particular way. 风险在于为$名称定义环境定义,而在运行时实际上并不会存在-以这种特定方式破坏强类型环境的目的。

What happens if you just say import 'jquery' and don't define a $ ? 如果您只是说import 'jquery'而未定义$什么? I imagine that you'd still be able to refer to the types to pass around a scoped, no-conflict jquery object, but that perhaps typescript will not define $ with that usage? 我想您仍然可以引用这些类型来传递有范围的,无冲突的jquery对象,但是也许打字稿不会用这种用法定义$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM