0

I m actually learning typescript, and I m facing some problems with internal modules.

In fact, I have three files :

index.ts in which I start my app

///<reference path='RouteManager.ts'/>
import RouteManager = RestifyRouting.RouteManager;

var myManager = new RouteManager();
myManager.init("superpath");

RouteManager.ts that manage my REST routes

///<reference path='RouteParser.ts'/>
module RestifyRouting {

    export class RouteManager {

        routeParser:RouteParser;

        constructor() {

        }

        public init(filePath) {
            this.routeParser = new RouteParser();
            this.routeParser.register("zfaf","callback");
            console.log(filePath);
        }
    }

}

RouteParser which has to parse some string to get some informations

module RestifyRouting {

    export class RouteParser {

        constructor() {

        }


        public register(path, callback) {
            console.log('super register');
        }

    }

}

I have a gulp file that creates my .js and d.ts files and it works great, except for the index.js file. The compiler tells my that RestifyRouting (which is my internal module) is undefined and I dont know why...

Can you help me ?

PS : every files are in the same folder, it's just a learning application.

Thanks for advance

4
  • What version of TypeScript are you using? Commented May 26, 2015 at 12:29
  • The 1.5 beta version Commented May 26, 2015 at 12:30
  • Can you share the tsc command parameters you're using to compile? (i.e. target, module) Commented May 26, 2015 at 12:33
  • Module : commonJS, target : ES3. These are the only parameters I use in my gulp file Commented May 26, 2015 at 12:34

1 Answer 1

2

As of TypeScript 1.5 the module syntax is aligned with ES6 module syntax and that is what I have been using as well...

You can remove any references to TypeScript modules and just export the classes directly

index.ts

import { RouteManager } from './RouteManager';
var myManager = new RouteManager();
myManager.init("superpath");

RouteManager.ts

import { RouteParser } from './RouteParser';
export class RouteManager {
    routeParser:RouteParser;
    constructor() {}
    public init(filePath) {
        this.routeParser = new RouteParser();
        this.routeParser.register("zfaf","callback");
        console.log(filePath);
    }
}

RouteParser.ts

export class RouteParser {
    constructor() {}
    public register(path, callback) {
        console.log('super register');
    }
}

Keeping modules

If you'd like to keep using internal modules then you have to be sure to export your module as well as the classes inside the module.

// RouteManager.ts
export module RestifyRouting {
  export class RouteManager{}
}

//index.ts
import { RestifyRouting } from './RouteManager';
//usage
var manager = new RestifyRouting.RouteManager();

Something to keep in mind is that you will not be able to import multiple items into the the same name.

// i.e.
import { RestifyRouting } from './RouteManager';
import { RestifyRouting } from './RouteParser';

NOTES the {} syntax in the import statement can allow multiple imports

{ Class1, Class2 }

The {} can be skipped if you exporting a default:

//Source (foo.ts):
export default class Foo{}
//Reference:
import Foo from './foo';
//usage:
class User {
  foo: Foo;
}
Sign up to request clarification or add additional context in comments.

3 Comments

There's no way to keep the namespace aspect ? I really like this way of work
You can, but it is not necessary... I'll add another example
A good explanation of why namespaces in external modules aren't necessary or even recommended: stackoverflow.com/questions/30357634/…

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.