Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm in the process of building a large-scale application and I'm trying to evaluate TypeScript and Angular2 as the base technologies to use.

Unfortunately, I'm having an issue with namespacing my own TypeScript files once I begin using Angular2.

Taking the example of the Heroes tutorial, I tried to make it match how I would see my own application being structured.

- app 
-- boot.ts

- Heroes
-- Components
--- HeroDetail.ts
--- HeroList.ts

-- Interfaces
--- IHero.ts

Within this structure, I then attempted to use namespaces for each logical area in order to group them. This resulted in IHero looking like this (for example):

namespace Heroes.Interfaces
{
  export interface IHero
  {
    Id: number;
    Name: string;
  }
}

Building the project in this structure works well until I attempt to add Angular into the mix by way of: import {Component} from 'angular2/core'; When this happens, the file loses all references to other code in the same namespace and there's no way to make it discover them.

I've seen this question asked about the same issue. There, the answer mentions "work arounds". For this reason I'm thinking it's POSSIBLE, but more than that I'm wondering why it ISN'T at the minute? Is this a bug or by design? I've been considering raising it on the TS GitHub but I would like to be a little better informed before going down that route.

Thanks for any advice!

share|improve this question
up vote 3 down vote accepted

Is this a bug or by design?

This is by design. Don't use namespaces if you are using file modules. Each file is already a module. And your whole project needs a module loader.

Read: Angular 2 Modules vs JavaScript Modules — Angular 2 Architectural Documentation

share|improve this answer
    
I understand that having an import statement at the top of the file turns it into a file module. What I'm asking is that does this HAVE to be the case? I want to build my project in namespaces because I will likely have a single built JS file for each section (it's an enterprise app and this will be done for security as it can be restricted based on access rights). In this case, namespaces seem more logical than single file modules? – Askanison4 Jan 19 at 9:08
    
What I'm asking is that does this HAVE to be the case. Yes – basarat Jan 19 at 21:33
1  
No. They are not useless. Use a module loader. Eg. webpack. Are you being deliberately obstinate and unhelpful That's a bit rude :-/ – basarat Jan 19 at 22:35
1  
Apologies. That was borne out of frustration. I had hoped that with a detailed question and comment that I could get some more information to work with. It had seemed that you had no intention of illustrating why the thinking was wrong, merely that it was. I will look into Webpack. Thank you – Askanison4 Jan 20 at 0:19
1  
A lot has changed in the last three years :) – basarat Jan 31 at 22:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.