0

I have an angular application, written in typescript whose config registration was just getting out of control, and huge - it took up about 3 pages.

So I broke it up into multiple config(fn) sections, and moved as much logic out of that page as I could and encapsulated specific behaviors into their own classes; it seemed like the smart way to go about cleaning it up.

each of the new classes looks something like this;

namespace config {
    export class http {
       constructor($httpProvider: ng.IHttpProvider){
           // $httpProvider configuration
       }
    }
}

so back in my main.ts, the file that creates my module and registers everything, I import them over.

import { http } from './config/http';
import { router } from ./config/router';
// etc.

but the namespace doesn't seem to be a part of this. I cannot call them such as...

config(config.http)
config(config.router)
// etc.

I've had to instead give them new alias when bringing them in;

import { http as configHttp } from './config/http';
import { router as configRouter } from './config/router';
// etc.

Why is this? Is there anything I can do to keep the namespace definition intact and use the simpler method I was aiming for?

1
  • A namespace creates a global variable, so you don't have to do an import statement. Just make sure that all of your JavaScript files are loaded in your html file. Commented Apr 18, 2016 at 16:43

1 Answer 1

1

The namespace you have is redundant if you're using import statements.

Here:

import { http } from './config/http';

You're asking only for the http class in that file so that's what you're getting.

Also, if anything it should be module config and not namespace config.
You should read Namespaces and Modules and more precisely Needless Namespacing.

Sign up to request clarification or add additional context in comments.

11 Comments

I have read both of those several times over; they don't really help. Besides, the document says quite clearly - the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions. - that's exactly what I'm doing. Logically grouping like constructs.
I'm open to other ideas, but the end goal is that I simply want to group them together while they remain in separate files. I can't see any good reasoning for keeping the class names different from the names you use to call them.
@Ciel if you keep reading that sentence: Because the module file itself is already a logical grouping, and its top-level name is defined by the code that imports it, it's unnecessary to use an additional module layer for exported objects. The namespacing is helpful when you don't use modules but just load everything in different .js files and then use them like new config.http() (without import)
I thought you had to import everything you wanted to use?
If I try just doing it without the import, I simply get an error cannot find name 'config'.
|

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.