Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically include AMD deps in Typescript AMD module?

Is there a way to import or annotate Typescript modules such that external AMD modules will automatically be included as dependencies when generating an AMD-compatible module?:

tsc --module AMD example.ts

I've tried to include both including a reference *.d.ts file, and exporting declare statements:

///<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 these:

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;
})

Would really like to avoid creating "fake" modules here.

It seems like a nice solution and usage would be to allow importing AMD modules directly:

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

but I don't know how feasible that is.

Edit:

And I've also tried this approach mentioned here: Import TypeScript module using only ambient definition for use in amd

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

but get these 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
like image 500
7zark7 Avatar asked Dec 12 '12 06:12

7zark7


1 Answers

This:

declare module 'jquery' { export var n; };

import $ = module('jquery');

export module foo {
    var n = $.n;
}

Will result in a proper define call:

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

Note that if you don't use an imported value ($ in this example) in a value position (as opposed to only in type positions), the compiler will optimize away that dependency.

like image 158
Ryan Cavanaugh Avatar answered Oct 27 '22 11:10

Ryan Cavanaugh