Automatically include AMD dampers in the AMD Typescript module?

Is there a way to import or comment on Typescript modules so that AMD external modules are automatically included as dependencies when creating an AMD-compatible module ?:

tsc --module AMD example.ts 

I tried to include both files, including the * .d.ts link file, and export the declaration instructions:

 ///<reference path='./lib/knockout-2.2.d.ts' /> export declare var $; export declare var _; export module example { export class Example { // whatever } } 

However, the generated module does not include:

 define(["require", "exports"], function(require, exports) { (function (example) { var Example = (function () { function Example() { } return Example; })(); example.Example = Example; })(exports.example || (exports.example = {})); var example = exports.example; }) 

I would like to avoid creating "fake" modules here.

It seems like a good solution and use would be to directly import AMD modules:

 var $ = import('jquery'); // This is a requirejs/AMD module, not a typescript file. 

but I do not know how this is possible.

Edit:

And I also tried this approach mentioned here: Import Typescript module using only the environment definition for use in amd

 import knockout = module("./lib/knockout-2.2.d.ts"); ... 

but get the following compiler errors:

 example.ts(1,32): The name '"./lib/knockout-2.2.d.ts"' does not exist in the current scope example.ts(1,32): A module cannot be aliased to a non-module type 
+8
requirejs typescript
source share
3 answers

In later versions of TypeScript, the correct way to do this ...

Example (this is jQuery)

Step 1: Download the definition file from NuGet (i.e. jquery.typescript)

Step 2: Here is the code (a reference comment is not required in Visual Studio):

 /// <reference path="scripts/typings/jquery/jquery.d.ts" /> import $ = require('jquery'); export module foo { var elem = $('#myid'); } 

As a result of JavaScript:

 define(["require", "exports", 'jquery'], function(require, exports, $) { (function (foo) { var elem = $('#myid'); })(exports.foo || (exports.foo = {})); var foo = exports.foo; }); 

Knockout

Some people had knockout problems ... The same technique works for a knockout ...

 /// <reference path="scripts/typings/knockout/knockout.d.ts" /> import ko = require('knockout'); export module foo { var obs = ko.observable('example'); } 

As a result of JavaScript:

 define(["require", "exports", 'knockout'], function(require, exports, ko) { (function (foo) { var n = ko.observable('example'); })(exports.foo || (exports.foo = {})); var foo = exports.foo; }); 
+3
source share

It:

 declare module 'jquery' { export var n; }; import $ = module('jquery'); export module foo { var n = $.n; } 

Will bring the correct define call:

 define(["require", "exports", 'jquery'], ... 

Note that if you do not use the imported value ( $ in this example) in the position of the value (as opposed to only in type fields), the compiler will optimize this dependency.

+4
source share

The response to Ryan worked, except that the new declaration hides the types specified in the comment file ".d.ts" with a triple comment.

To overcome this, you can try to modify the declaration as follows:

 declare module 'knockout' { export = knockout; } 

I have not tested by knockout, but the solution should work with this as well.

0
source share

All Articles