在TypeScript中使用import / require来获取接口声明(Use import/require in TypeScript to get interface declarations

编程入门 行业动态 更新时间:2024-10-25 12:25:07
在TypeScript中使用import / require来获取接口声明(Use import/require in TypeScript to get interface declarations)

如果我使用/// <reference path="..."/>语法引用TypeScript声明文件(例如jquery.d.ts),那么我需要确保通过其他方式加载相应的库,即只引用.d.ts文件不会加载库。

当我使用它时,有什么方法可以使TypeScript为库生成一个require()调用? 如果我不使用AMD / requirejs,我可以手动调用require ,但是我希望能够与AMD合作。

这样做的好处是我的依赖不会在两个地方被定义。 从.ts文件引用库足以确保加载,而不必在我的HTML中手动维护依赖列表。

更新 :我打开了一个新的问题 ,澄清我的确切情况。 因为我没有提供所有必要的细节,所以我想给出我原来的问题的答案。

If I reference a TypeScript declarations file (ex. jquery.d.ts) using the /// <reference path="..."/> syntax, it's up to me to make sure I load the corresponding library by some other means, i.e. just referencing the .d.ts file doesn't load the library.

Is there a way to make TypeScript generate a require() call for the library when I use it? If I wasn't using AMD/requirejs I could just call require manually, but I'd like to get this to work with AMD.

The advantage of this is that my dependencies wouldn't be defined in two places. Referencing the library from a .ts file would be enough to make sure it loads, rather than having to maintain the list of dependencies manually in my HTML.

Update: I opened a new question that clarifies my exact situation. I want to give credit for the answer to my original question since I didn't give all the necessary details.

最满意答案

是的,TypeScript支持“外部”模块,它们基本上是一流的AMD或CommonJS模块。 例如:

MyLib.ts

export function foo() { return 'bar' }

MyProj.ts

import lib = module('./MyLib.ts') lib.foo(); // returns bar

用“--module amd”编译它,你将得到正确的模块并且需要为你生成语法。

Yes, TypeScript supports "external" modules, which are basically first class AMD or CommonJS modules. For example:

MyLib.ts

export function foo() { return 'bar' }

MyProj.ts

import lib = module('./MyLib.ts') lib.foo(); // returns bar

Compile this with "--module amd" and you'll get the proper module and require syntax generated for you.

更多推荐

ts,TypeScript,电脑培训,计算机培训,IT培训"/> <meta name="description&quo

本文发布于:2023-08-07 13:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   声明   import   TypeScript   require

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!