9

I have a project (nodejs) written in typescript I got two file that define User class, none of the is exported and though they are isolated i get duplicate error from the typescript compiler on both of these files

examples/hello_world.ts(3,7): error TS2300: Duplicate identifier 'User'. 
examples/models/user.model.ts(2,7): error TS2300: Duplicate identifier 'User'.

Any ideas? Thanks

4
  • Could you show us the content of these files ? Commented Apr 19, 2017 at 9:28
  • Do you use any module management? Namespaces? Commented Apr 19, 2017 at 9:31
  • Julien: Just am empty declaration of User class. (ill post the contend in a bit when ill be back to the pc) Commented Apr 19, 2017 at 9:35
  • Vadim: I use commonjs in the tsconfig.json and no namespaces Commented Apr 19, 2017 at 9:37

2 Answers 2

15

How to force a source file to be a module

If your source file doesn't contain any top level import or export, just add the following line:

export {};

Notice: There is a proposal on this subject but currently in stage 1, we have to wait.


Original answer: Use classic imports and exports

Use the ES6 syntax for modules, with import and export:

// models/user.model.ts
export class User {
}

// hello_world.ts
import { User as UserModel } from "./models/user.model"
export class User {
}

See the section "Renaming imports and exports" in the article: ES6 In Depth: Modules, from Mozilla.

Sign up to request clarification or add additional context in comments.

6 Comments

This is the exact issue. I do not want to export them. I wand each to have it own isolated User class
@Dimkin If you don't export them, there is no conflict between them. But you have to export or import something in order to process these files as modules. If you have none, can you try to add export let undefined in your files?
I dont want export them. And from nodejs runtime there is no conflict as expected. But typescript compiler says there is. And this is whats weird
@Dimkin Try to add export let undefined in your TypeScript files.
Hi, Have you figured out why this is happening ? I expected there would be no conflicts since we define them in two separate files if seen from a commonjs perspective. I donnt understand why ts is treating them as if they were global definitions.
|
7

Due to compatibility with web-platform TypeScript-team decided to treat scripts without explicit imports and exports as just plain scripts:

Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

https://www.typescriptlang.org/docs/handbook/modules.html#introduction

For environments like Node.js, it is not so convinient. There is an issue in the TypeScript repo regarding this https://github.com/microsoft/TypeScript/issues/18232.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.