Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I have this ts module:

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

and I compile it with the amd option I can use it quite easily from a 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;
})

There is no way to use it easily. The simplest possible 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 because of error http://requirejs.org/docs/errors.html#notloaded . Since "jsmodule" was not passed to the define call in the generated typescript.

The two workarounds I have

  • don't use import / export (language features lost)
  • use require([]) (still can't export something that depends on the require([]) call)

have limitations : https://github.com/basarat/typescript-requirejs . Is there another way? If not can you vote here : https://typescript.codeplex.com/workitem/948 :)

share|improve this question

1 Answer 1

up vote 3 down vote accepted

If you want to load in a JavaScript module you could always use the (badly documented) amd-dependency tag:

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

This will put jsmodule in the dependency array of your define call.

And then provide a declaration file in which you would simply state

module useme {
    function say(): void;
}
share|improve this answer
1  
apparently an undocumented feature, and does not work with my tsc version 0.9.0 alpha. Let me uninstall / install to version 0.8 –  basarat Apr 25 '13 at 12:12
    
Aww shoot, they removed this in 0.9?! I'm using 0.8.1.1. –  Anzeo Apr 25 '13 at 12:20
2  
Works in 0.8.3 . But not in 0.9.0alpha –  basarat Apr 25 '13 at 12:47
    
Good to know. I hope they include similar functionality in 0.9+ as I rely heavily on this feature –  Anzeo Apr 25 '13 at 12:49
2  
0.9a is supposed to have a few utility bits missing. Hopefully amd-dependency is one of these. I do hope they haven't removed it permanently. –  JcFx Apr 25 '13 at 16:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.