0

I am trying to organize my TS project, and I have moved most of my classes and interfaces in to a /models directory. In an effort to make things easier on myself/future developers, I figured making a toplevel models.ts file that imported all the models and then reexported them would make things easier.

To that end, I thought of making a namespace/module called Models and then exporting that namespace/module. However, I ran in to issues trying to link up what I had imported in to the exported module, like so:

import {Args} from "./models/args";
import {Response} from "./models/response";
import {Inventory} from "./models/inventory";

export module Models {
    // Trying things like:
    // this.Args = Args --- nope, not a class
    // Args = Args -- nope
    // export Args -- nope
    // export class Args -- nope
    // export class ArgsModel extends Args -- nope
    //
    // This works, but it's renaming, and it feels hackish.
    export class ArgsModel extends Args {}
    //
    // import export Args from "./models/args" -- nope, cant import from within a module
}

Searching for a while, I found a lot of information about modules, but nothing that addressed this in particular, which has made me realize this is probably not a pattern TS uses. To that end, I found a similar question asked here How to export multiple imported classes as a module that yielded an answer suggesting to do something like this:

export * from "./models/args"
export * from "./models/response"
export * from "./models/inventory"

However, I also feel I've gone so far down this rabbit hole that I want to figure out how I would go about my original intention simply so I can understand typescript a bit better.

How would one export a single namespace that contained classes, interfaces, and modules that have been imported?

Community
  • 1
  • 1
Mike Manfrin
  • 2,722
  • 2
  • 26
  • 41

1 Answers1

1

I thought of making a namespace/module called Models and then exporting that namespace/module.

You file is a module. You don't need to use a namespace. More : https://basarat.gitbooks.io/typescript/content/docs/project/namespaces.html vs. modules : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

I also feel I've gone so far down this rabbit hole that I want to figure out how I would go about my original intention simply so I can understand typescript a bit better.

Modules imports / exports need to be statically analyzable and therefore are constrained to be at the root of a file.

You can however re-export them from a namespace (if you really really really want to). Just note that you need to rename the root one different otherwise within the module it will get shadowed.

import {Args as ArgsModel} from "./models/args";

export namespace Models {
   export const Args = ArgsModel;
}
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thank you -- follow up question: is this a pattern I should avoid? I'm doing something similar with Controllers (where I'll have `Controllers.[SomeModel].etc`), but it feels strange that I have to set the [SomeModel] module as `export module Controllers.[SomeModule]` rather than the `Controller` bit being implicitly the parent module. – Mike Manfrin May 20 '16 at 20:40
  • Using `namespace` + external modules is a bad idea. Just use `modules` unless you really really need to use namespaces – basarat May 23 '16 at 00:53