Skip to content

Duplicate identifier error when naming a class 'Node' #16143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pb82 opened this issue May 30, 2017 · 6 comments
Closed

Duplicate identifier error when naming a class 'Node' #16143

pb82 opened this issue May 30, 2017 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@pb82
Copy link

pb82 commented May 30, 2017

TypeScript Version: 2.3.3
Node.js Version: 7.10.0

Code

class Node {}

Safe the file as index.ts and run tsc index.ts. No tsconfig.json.

Expected behavior:
Compiles without error.

Actual behavior:
Compiler returns the following error:

../../../usr/lib/node_modules/typescript/lib/lib.d.ts(12520,11): error TS2300: Duplicate identifier 'Node'.
../../../usr/lib/node_modules/typescript/lib/lib.d.ts(12572,13): error TS2300: Duplicate identifier 'Node'.
index.ts(1,7): error TS2300: Duplicate identifier 'Node'.

Renaming the class makes it pass.

@kitsonk
Copy link
Contributor

kitsonk commented May 30, 2017

Because Node is already part of the global namespace and cannot be redeclared. It needs to be part of a module, so it is not part of the global namespace.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 30, 2017
@pb82
Copy link
Author

pb82 commented May 30, 2017

That makes sense, thanks. Closing it now.

@pb82 pb82 closed this as completed May 30, 2017
@brandonbloom
Copy link
Contributor

What do you mean by "It needs to be part of a module"? I ran in to this exactly name collision, but I've got my class definition in a .ts file that is being loaded via import. My tsconfig.json has the module system set to "commonjs".

@kitsonk
Copy link
Contributor

kitsonk commented Sep 5, 2017

You would need to provide more details. What does your module look like? If it doesn't have any imports or exports then TypeScript won't treat it as a module.

This will error while this works:

-class Node {
+export class Node {

}

@brandonbloom
Copy link
Contributor

If it doesn't have any imports or exports then TypeScript won't treat it as a module.

Whoa. I did not realize that.

This particular module will have no imports and did not yet have any exports, since I had just started writing it. I wasn't planning to export the Node type, but was going to export some functions that used it.

Thank you!

@kitsonk
Copy link
Contributor

kitsonk commented Sep 5, 2017

As just discussed in #18232, if you need have something that you want to be considered a module without any imports or exports you can simply use:

export { }

Which will end up being stripped when the module is emitted.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants