1
votes

I am switing to using AMD compilation rather than typescript internal modules.

I have a project full of typescript classes and interfaces that I have successfully optimized into a requirejs file full of requirejs defines.

for example

class A { ... } export A;

Other classes reference the file and import the class fine.

In another project I pull in the R.js optimized file using bower and reference it in require_config.js and load the file ok.

Within this other project I want Class B to reference class A but class B is not in the file path.

What do I need to do? I guess I need to generate d.ts files that I reference in Class B but how do I do import ClassA = require("ClassA") when ClassA is not in the file path of the second project.

James

2
I don't see where your example shows that you're using requireJS. Anyway you should be able to access classB using its relative path from the site root.xlecoustillier
if I compiled Class A with -m AMD I would get a js file that looked like this:define(["require", "exports", dependencies], function (require, exports, dependencies) { return ClassA });jmc42
but class B is not in the file path ... class A or B?Paleo
sorry my mistake. I edited the question - Class B does not have Class A in the file path.jmc42

2 Answers

0
votes

Yes, the second project can use a definition file .d.ts:

// ---------- projectA.d.ts ----------
declare module "projectA" {
    export class A {
        // ...
    }
}

// ---------- ClassB.ts ----------
import projectA = require("projectA"); // Use the definition in projectA.d.ts
var a = new projectA.A();

See the section "Ambient External Modules" in the Handbook.

You can automatically generate a single definition file bundle from your projectA using dts-generator.

0
votes

Following my comment that projectA was undefined when I did var a = new projectA.A(), I discovered that the reason for this lay in the way that the R.js optimized file (projectA.js) has been built - it needed an insertRequire added to the R.js build. This caused a require["projectA"] to be called when projectA was loaded and projectA was instantiated properly.

There are a quite a few moving parts here. It is a pity that typescript external modules and internal modules work so differently. Depending on the syntax declare module "SomeModuleName" and declare module SomeModuleName to distinguish via use of quotes between external and internal modules is confusing.

It would be great if the syntax was the same and the compiler -m flag handled all the underlying detail.