0
votes

This is a Typescript question about namespaces and file management. I have a number of interfaces and classes under a single namespace. I want to segment each class or interface into its own .ts file and then be able to aggregate the files so that I can just declare the namespace in some consuming code and then have accesses to the interfaces and classes within the namespace. For example, the first file, icontact.ts:

export namespace mynamespace {
  export interface IContact {
    firstName: string;
    lastName: string;
  }
}

Second file, contact.ts:

import {IContact} from './icontact.ts';
export namespace mynamespace {
  export class Contact implements IContact {
    firstName: string;
    lastName: string;
    constructor(firstName: string, lastName: string){
      this.firstName = firstName;
      this.lastName = lastName:
    } 
  }
}

I will really appreciate any help that anyone can offer.

Thanks in advance.

1

1 Answers

0
votes

namespaces in Typescript are a way to describe how modules are structured, but they don't generate any code that automatically does this for you.

What you will probably want to do, is to create a single file (perhaps index.ts in some directory), and from that file import everything you want in your 'namespace', and re-export.

Example of such an index.ts file:

export { Foo } from './foo';
export { Bar } from './bar';

Now if something uses this module, they could reference it as such:

import * as MyNamespace from './some/directory';

let foo: MyNamespace.Foo;
let bar: MyNamespace.Bar;