TypeScript across multiple assemblies

Topics: General
Aug 14, 2013 at 1:52 PM
Edited Aug 14, 2013 at 1:52 PM
Is there a way to define external modules in different assemblies and reference them.

Example:

Assembly1 - (location scripts/)
export class Animal {
      feet: number;
}
Assembly2 - (location scripts/)
import asm1 = module(???????);

class Dog extends asm1.Animal {
     barkType: string;
}
Aug 14, 2013 at 2:10 PM
Sure, jmwheeler
import asm1 = module('scripts/Animal');
Have a look at this blog on using AMD assemblies with require.js : http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/
Aug 14, 2013 at 2:17 PM
Maybe I'm missing it but I don't see how it would be able to find a reference to Animal when it's in a completely different assembly/project.
Aug 14, 2013 at 2:42 PM
Edited Aug 14, 2013 at 2:42 PM
When you say different assembly, are you talking about a different web-site ?
Assume yes, then that you have http://site-a/scripts/Animal.js, and you are trying to import this to http://site-b/NeedAnimal.ts, then I also can't see how you can use the module keyword.
As far as I understand, the TypeScript compiler uses the module('...') keyword to load the TypeScript file at compile-time, and then compiles the resultant javascript to use require as follows:
require(['scripts/Animal'],  function(_scripts_animal) {
    // scripts_animal is valid here.
});
In this case, you would have to treat the scripts/Animal.js as any other .js file and create a module definition ( Animal.d.ts ) to allow for compilation - then include the http://site-a/scripts/Animal.js as a script resource.

Hope this helps.
Aug 14, 2013 at 2:49 PM
When I say assembly, I'm referring to having different .dll files with libraries of typescript files in them. I'm working on a large scale project and attempting to have portions of the code that are reusable across different projects.

I'm sure I'm not the only person who has this need which makes me think that I am either going about this the wrong way or don't know exactly what to search for in order to see what other people are doing.

Example:

Assembly1.dll
 - /Scripts/Animal.ts
Assembly2.dll
 - /Scripts/Dog.ts

Coordinator
Aug 16, 2013 at 2:16 PM
TypeScript currently only supports two styles of referring to other files. Imports, which set up code to work with a module loader like AMD or CommonJS, and ///<reference path="..."/>, which tells the compiler that it will be up to you to reference it.

If you've worked out how the dlls are going to have the underlying JavaScript loaded so the symbols are at known locations, the other step is to provide this type information. When doing projects where there are distinct libraries/modules/etc, the recommended way is to create a .d.ts file that represents the API of each module, and then use the ///<reference> notation to reference these .d.ts files. The TypeScript compiler can automatically generate these from source code given the --declaration flag.
Aug 16, 2013 at 6:35 PM
I think I'm going about this the wrong way. It's too much of a "Windows App" based approach. I think I'll create a Nuget package for each different project and have each project pull in any package that it requires.