1
votes

I have a set of classes basically all the models/enums. I want to put all these classes under the same namespace, that way I don't need any import statements.

Now there are other business layer classes that need to use these models.

My problem is that in order for my business layer to use models I need to export the models namespace, but when I do that then the model classes cannot share anything.

Is there a way export a namespace that spans multiple files

namesace models {
  export class Employee {
  }
}

namespace modesl {
  export class Manager {
    //using Employee class
    //no need to import because they are in same namespace
  }
}

//need to import employee to services namespace
//This gives the error cannot find name Employee
import { Employee } from '../models/Employee'

namespace services {
  //Use Employee and Manger 
}
1
What you are describing will work perfectly fine as long as you don't import or export anything in any of the files that you want to have contribute to the global namespace. - Aluan Haddad
I have updated my question with an example, I am not importing exporting anything in my models namespace but when trying to import any class from that namespace into another namespace it does not work - vikas mittal
You shouldn't do this. It will cause you all sorts of file ordering and module loading problems since you models are classes and not interfaces. If they represent server request/response values, make them all interfaces or types. Your code will improve in quality and, the approach you have attempted here will actually be possible. Note that using classes to represent values that are serialized and deserialized is a serious design error. - Aluan Haddad

1 Answers

0
votes

Is there a way export a namespace that spans multiple files

Easy:

export const NewName = GlobalNamespaceName;