Typescript Deploying AMD with Javascript / RequireJS

If I have this ts module:

export function say(){
    console.log("said");
}

and I will compile it using the amd option. I can easily use it with ts client:

import foo = module("tsmodule")
foo.say();

export var x = 123;

However, if I have javascript equivalent to the ts module:

define(["require", "exports"], function(require, exports) {
    function say() {
        console.log("said");
    }
    exports.say = say;
})

Unable to use it easily. The simplest solution:

// of course you can use .d.ts for requirejs but that is beside the point
declare var require:any;

// will fail with error module has not been loaded yet for context
// http://requirejs.org/docs/errors.html#notloaded
var useme = require("jsmodule")
useme.say();

export var x = 123;
import foo = module("tsmodule")
foo.say();

fails due to http://requirejs.org/docs/errors.html#notloaded error . Since "jsmodule" was not passed to the define call in the generated typescript.

Two workarounds that I have

  • do not use import / export (language features are lost)
  • use require ([]) (still cannot export something that depends on calling require ([])

: https://github.com/basarat/typescript-requirejs. ? : https://typescript.codeplex.com/workitem/948:)

+5
1

JavaScript, amd-dependency ( ):

/// <amd-dependency path="jsmodule" />

jsmodule .

,

module useme {
    function say(): void;
}
+3

All Articles