Skip to content
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

[error TS2300: Duplicate identifier] Global references #1039

Closed
br1anchen opened this issue Nov 3, 2014 · 10 comments · Fixed by #1131
Closed

[error TS2300: Duplicate identifier] Global references #1039

br1anchen opened this issue Nov 3, 2014 · 10 comments · Fixed by #1131
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@br1anchen
Copy link

Hi,

I just updated the typescript compiler from 1.0.1.0 to 1.1.0.1.

Then when I using the Webstorm Typescript Transpiling, it keeps reporting TS2300: Duplicate identifier.
I investigate a bit, it seems like the references is the problem.

In our current project, we include all the .ts files and external library .d.ts files in one references.d.ts file and include a single references.d.ts file for each .ts file we coded.
This reference mechanism works just fine on tsc 1.0.1.0

But on 1.1.0.1, it seems like it can not do self reference any more. Then I try to make specific reference for each .ts file. However it still get some TS2300: Duplicate identifier in some classes when the dependencies depend on another same dependency. It seems like it will reference the same dependency twice to cause Duplicate identifier error.

eg:

A.ts:
/// <reference path="D.ts" />

B.ts:
/// <reference path="D.ts" />

C.ts:
/// <reference path="A.ts" />
/// <reference path="B.ts" />

Because I am using AngularJS framework, so it is quite common that A and B are services to inject D service, C as controller to inject A and B services.

In this scenario, typescript compiler report TS2300: Duplicate identifier 'D' when it compile C.ts

PS: I know this references issue will not occurs in VS since it has some 'magic' implicant reference mechanism to reference all the .ts/ .d.ts files. I hope it is not the Webstorm issue, because it seems like compile issue more or less.

Also, it will be great if there is some standard Typescript coding style to do the proper dependencies reference when people are using Webstorm or other IDE than VS. Maybe some auto fixing dependencies reference as plugin would be better.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 3, 2014

@br1anchen loops in the reference graphs are still allowed. the example you have should still work in 1.1.0.1. There can be something else that is causing your issue. can you give a try with the latest in master? it will report you the errors on all instances of the duplicate definition.
alternatively, if you can share a minimal repro of the issue i can take a look.

@danquirk danquirk added the Needs More Info The issue still hasn't been fully clarified label Nov 3, 2014
@Maks3w
Copy link

Maks3w commented Nov 10, 2014

I've the same problem (Verified with master).

I've a common file where all TypeScript references are included (tsd.d.ts)

// FieldManager.ts
/// <reference path="../../../typings/tsd.d.ts" />

class FieldManager {
...
}
// tsd.t.ts

/// <reference path="../src/ts/Manager/FieldManager.ts" />
...

Error message when compile FieldManager.ts:

../../../src/ts/Manager/FieldManager.ts(6,7): error TS2300: Duplicate identifier 'FieldManager'.
FieldManager.ts(6,7): error TS2300: Duplicate identifier 'FieldManager'.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 10, 2014

I see it now thanks @Maks3w for the additional info. so the problem is how the compiler does relative path computation. if you give the compiler relative paths, it will not turn them to absolute, so it will just use ..\ to move up if needed. the result is that the same file can have more than one relative paths. and then included multiple times.

A workaround for now, is either you call the compiler from your root folder ("src" in the above example) or just use absolute paths for files passed on the command line compiler.

This fails:

d:\test\src\ts\Manager>tsc FieldManager.ts
../../../src/ts/Manager/FieldManager.ts(4,7): error TS2300: Duplicate identifier 'FieldManager'.
FieldManager.ts(4,7): error TS2300: Duplicate identifier 'FieldManager'.

Absolute path, works

d:\test\src\ts\Manager>tsc d:\test\src\ts\Manager\FieldManager.ts

Relative path to the root works

d:\test\src\ts\Manager>tsc......\src\ts\Manager\FieldManager.ts

At the root directory works

d:\test>node tsc src\ts\Manager\FieldManager.ts

@mhegazy mhegazy added Bug A bug in TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Nov 10, 2014
@sophiajt sophiajt added this to the TypeScript 1.4 milestone Nov 10, 2014
@segrey
Copy link

segrey commented Nov 11, 2014

@mhegazy Thanks for providing workarounds. Passing absolute path to TypeScript compiler works fine. I'm just wondering if it is safe enough to enable this behavior by default in WebStorm (I'm maintaining WebStorm TypeScript transpiling)? Will it work with previous versions of TypeScript compiler? Thanks.

@br1anchen
Copy link
Author

Hi, Thanks for @mhegazy and @Maks3w . Relative path is the problem.

@segrey : I am using TypeScript transpiling file watcher in my project.
I used to set Arguments: --sourcemap $FileName$ --target ES5 and Working directory : $FileDir$

After @mhegazy work around solution:
I now set Arguments: --sourcemap $FilePath$ --target ES5 and Working directory : $FileDir$
Then it works well.

I think it is ok to use $FilePath$in arguments, and keep $FileDir$ as working directory, then in the ts file relative path will be still working, and without ts compiler errors

@buckhorn-foxpert
Copy link

My master ts file is not in the root, but down a couple of levels. In Webstorm I had to change the Working Directory to $ProjectFileDir$ to make tsc.cmd work

@davidleureka
Copy link

So to clarify, the is the recommended approach to have one giant tsd.json file and corresponding typings folder for you entire project? If I try to break it up and have multiple folders each with their own tsd.json and typings then I still run into this issue for libraries that are used in multiple places (such as bluebird or lodash).

@EBMike
Copy link

EBMike commented Jan 13, 2016

Yeah this is far from fixed... it shouldnt throw this error for reference paths

@mhegazy
Copy link
Contributor

mhegazy commented Jan 13, 2016

@davidleureka and @EBMike this issue has been closed over a year now. I am finding hard to understand the context between your comments and the original issue. please file a new ticket and include relevant details.

@davidleureka
Copy link

done

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants