1

I'm trying to split module to several files. I followed this question but with no luck, classes within same module but in different files seems to be unaccessible to me.

I'm using TypeScript 0.8.2 plugin for Visual Studio 2012

/* filesystem */
- /core
- - Router.ts
- - Server.ts
- /DefinitelyTyped
- - ..
- index.ts


/* file Router.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
import Url = module("url");

module Core {
    export class Router {
        static route(url: string) {
            var pathname: string = Url.parse(url).pathname;
            var query: string = Url.parse(url).query;
            console.log("About to route to " + pathname);
        }
    }
}

/* file Server.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
///<reference path="Router.ts"/>

import Http = module("http");

module Core {
    export class Server {

        private server: Http.Server;
        private port: number;

        constructor(port: number) {
            this.port = port;
            this.server = Http.createServer(this.onRequest);
            this.server.listen(this.port);
            console.log("Server started at http://127.0.0.1:" + this.port + "/.");
        }

        private onRequest(request: Http.ServerRequest, response: Http.ServerResponse) {
            var url: string = request.url;

            console.log("Received request: \"" + request.url + "\".");

            Router.route(url); //problem: Name 'Router' doesn't exist in this scope...

            response.writeHead(200, { "Content-Type": "text/plain" });
            response.write(url);
            response.end();
        }

        public getPort(): number {
            return this.port;
        }
    }
}

So, what am I missing?

1 Answer 1

4

As a general rule, never mix and match import/export with /// <reference> tags to files that contain anything other than declarations (i.e. only reference .d.ts files if you use import/export anywhere in your code). What you want is this (see NB additions/deletions):

/* file Router.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
import Url = module("url");

// NB Removed containing module for clarity
export class Router {
    static route(url: string) {
        var pathname: string = Url.parse(url).pathname;
        var query: string = Url.parse(url).query;
        console.log("About to route to " + pathname);
    }
}

/* file Server.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
// NB Removed reference

import Http = module("http");
import Router = module("./Router"); // NB Added

module Core {
    export class Server {

        private server: Http.Server;
        private port: number;

        constructor(port: number) {
            this.port = port;
            this.server = Http.createServer(this.onRequest);
            this.server.listen(this.port);
            console.log("Server started at http://127.0.0.1:" + this.port + "/.");
        }

        private onRequest(request: Http.ServerRequest, response: Http.ServerResponse) {
            var url: string = request.url;

            console.log("Received request: \"" + request.url + "\".");

             // NB first 'Router' is the import name at the top of this file, feel free to change
             // NB second'Router' is the name of the class from that module
            Router.Router.route(url);

            response.writeHead(200, { "Content-Type": "text/plain" });
            response.write(url);
            response.end();
        }

        public getPort(): number {
            return this.port;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So packaging similar to Java is not possible?
I don't get it. Router.Router is so ugly. Besides, what @blootoon is originally doing is what it seems is in the tutorial for separate files here

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.