0

I am trying to understand how to organize my code with Typescript modules. Here is my following architecture:

~ src app SubModule1 (folder) Class1.ts components Component1.ts (require to create class 1) MainModule.ts

// Class1.ts

module MainModule.SubModule1 {
    export class Class1 {
        // some code
    }
}

// Component1.ts

import MainModule = require("MainModule");
export class {
    constructor() { var test = new MainModule.SubModule1.Class1; }
}

// MainModule.ts

module MainModule {
    export var config = "config";
}

export = MainModule;

The Component1 class find the MainModule fine, but I cannot access the SubModule1 class. Could someone explain me what I am doing wrong?

Thank you.

2

1 Answer 1

1

The quick answer is that you need to import it too, but the long answer is that you should drop the module declarations when using external modules...

// Class1.ts

export class Class1 {
    // some code
}

// Component1.ts

import MainModule = require("../MainModule");
import SubModule = require("../app/SubModule1/Class1");

export class {
    constructor() {
        var test = new SubModule.Class1();
    }
}

// MainModule.ts

export var config = "config";
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this answer, so I tried to follow your code but in component1.ts, the import of the second file I have the following error : Module cannot be aliased to a non-module type
Also, why should I not specify the name of the modules by myself, in order to make the code discoverable?
@user2465083 because each file is a module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.