如果我使用/// <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 barCompile this with "--module amd" and you'll get the proper module and require syntax generated for you.
更多推荐
ts,TypeScript,电脑培训,计算机培训,IT培训"/> <meta name="description&quo
发布评论