0

Maybe I am not using the namespaces correctly with Typescript but here is the problem I am trying to solve.

When creating a class, I need to reference all my other files by using a require as followed.

import DependencyModule = require("Path.../DependencyModule");
import DependencyClass = DependencyModule.DependencyClass;

Each time I need to add these 2 lines if I want to be able to construct class like this:

var class = new DependencyClass();

Because my project is intended to be big, and I already need several classes I was will to have a file that I called ClassStore.ts that would have a reference to all the classes so that I only need to reference this file once.

import ModulesStore = require("ClassStore");
var class = new ModulesStore.DependencyClass();

Is it possible to do this? Am I missing something with namespaces?

Please note that I try to export import classes and interfaces from files containing multiple export. One for the interface and one for its implementation.

Here is a typical file architecture for my project (DependencyClass.ts):

export interface IDependencyClass { ... }
export class DependencyClass { ... }

Here is what I tried in the "ClassStore":

// ClassStore.ts
export import DependencyModule = require("DependencyClass");

// Other file needing the DependencyClass
import ClassStore = require("ClassStore");
var class = new ClassStore.DependencyModule.DependencyClass();

I receive the following error : Uncaught TypeError: Cannot read property 'DependencyClass' of undefined.

Thank you for your help.

user2465083
  • 595
  • 3
  • 12

1 Answers1

0

Is it possible to do this? Am I missing something with namespaces?

Yes. From DependencyModule, export the class with =

class Foo{}
export = Foo;

Use an index.ts (or whatever you want to call it) that import/exports all the classes e.g.

export import DependencyModule = require("Path.../DependencyModule");
export import DependencyModule2 = require("Path.../DependencyModule2");

Then you can do :

import Index = require("Path.../Index");
var foo = new Index.Foo();
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Hi, thank you very much for the answer. I forgot to mention that I usually export 1 interface and 1 class from a single file (for code navigation). This mean that I cannot receive a single class directly from a file. Please have a look to the question, I have updated it with more details. – user2465083 Aug 09 '14 at 10:08