Can I import and export same-name class and namespace simultaneously in TypeScript?

1.4k views Asked by At

Consider class (e. g., ExampleUtil) in the file ExampleUtil.ts. Currently types which ExampleUtil uses, stored in the file ExampleUtil__TYPES.ts. No problems when import them into ExampleUtil.ts; currently, there is no need for namespace.

When I begin to use ExampleUtil in other projects, it turned out that types from ExampleUtil__TYPES.ts is context-dependent and it's better to en-capsulate them to namespace. However, the name ExampleUtil already using by class, so I need to select the other name for namespace or resolve this conflict by other method.

In my library, I organized the type definitions as:

import ExampleUtil from "./ExampleUtil/ExampleUtil";
import { Type1, Type2 } from "./ExampleUtil/ExampleUtil__TYPES"
import OtherUtil from "./OtherUtil/OtherUtil";
import { Type3, Type4 } from "./OtherUtil/OtherUtil__TYPES"
// other classes, types and functions from library


/** Re-export for other project.
 *  Unlike "lodash", where each function is in separate file, herewith 
 *  all files are in same directory, here the developers can organize source files
 *  by directories, but users does not need to know these directories, because 
 *  they can get everything what they need from single "my-lib.js" file like below:
 *  import { ExampleUtil, Type1, Type2 } from "my-lib";
 *  All "my-lib" classes and functions those has not been used, will not be included
 *  by Webpack to production bundle.
 */
export {
  ExampleUtil, Type1, Type2,
  OtherUtil, Type3, Type4,
  // ... other classes, types and functions from library
};

If we wrap Type1 и Type2 no namespace ExampleUtil, the conflict will occur already here, in my-lib.d.ts: we are import class and same-name namespace, then we need to re-export both.

So, can I write the code like

//                                Namespace
const parameterForExampleUtil: ExampleUtil.Type1 = { /* ... */ };
//             Namespace         Class
const result: ExampleUtil.Type2 = ExampleUtil.getSomething(parameterForExampleUtil);

and if no, which name I may select for namespace? Some good practices from other programming languages allows avoid conflicts between class name and respective namespace? I know that using prefix I for interfaces, is the practice like this for namespaces?

0

There are 0 answers