Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Angular with TypeScript and Namespaces

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!

like image 484
aaron-bond Avatar asked Jan 18 '16 10:01

aaron-bond


People also ask

Should you use namespaces in TypeScript?

For most projects we recommend using external modules and using namespace for quick demos and porting old JavaScript code. TSLint has a predefined rule to avoid namespaces. The same opinion is found in many places around Stack Overflow: I wouldn't recommended namespace nor mixing it with module source code.

How do namespaces work in TypeScript?

The namespace is used for logical grouping of functionalities. A namespace can include interfaces, classes, functions and variables to support a single or a group of related functionalities. A namespace can be created using the namespace keyword followed by the namespace name.

What is the difference between namespace and module in TypeScript?

Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .

What is name space in TypeScript?

The namespace is a way which is used for logical grouping of functionalities. It encapsulates the features and objects that share common relationships. It allows us to organize our code in a much cleaner way. A namespace is also known as internal modules.


1 Answers

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

like image 117
basarat Avatar answered Sep 21 '22 02:09

basarat