7

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

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

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!

1 Answer 1

5

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

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

8 Comments

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?
What I'm asking is that does this HAVE to be the case. Yes
No. They are not useless. Use a module loader. Eg. webpack. Are you being deliberately obstinate and unhelpful That's a bit rude :-/
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
A lot has changed in the last three years :)
|

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.