Use import / require in TypeScript to get interface declarations

If I reference the TypeScript declaration file (for example, jquery.d.ts) using the syntax /// <reference path="..."/> , it is up to me to make sure that I load the appropriate library in other ways, that is, just referring to. The d.ts file does not load the library.

Is there a way to make TypeScript generate a require() call for the library when I use it? If I had not used AMD / requirejs, I could just call require manually, but I would like this to work with AMD.

The advantage of this is that my dependencies will not be defined in two places. A link to the library from the .ts file will be enough to ensure that it is loading, instead of manually maintaining the dependency list in my HTML.

Update : I opened a new question that clarifies my exact situation. I want to pay tribute to the answer to my original question, since I did not give all the necessary details.

+8
javascript typescript js-amd
source share
2 answers

Yes, TypeScript supports "external" modules, which are mostly 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 will get the right module and require syntax from you.

+6
source share

I wrote something about this on my blog. You can also find an example on GitHub .

The solution has been explaining for quite some time, but I mainly use pads with Require.JS to determine the name of the module representing the Javascript library I want to load. Then I create a TypeScript file with the same name so that the TypeScript compiler generates Javascript code that can use the JS library I want. This is actually not the case, but please read the post, and I think it will make more sense.

+1
source share

All Articles