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

Duplicate identifier error when dependent project supplies its own type definitions for a shared library #10968

Closed
mattyork opened this issue Sep 17, 2016 · 14 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@mattyork
Copy link

TypeScript Version: 2.0.2

  • I have two directories containing typescript projects: proj1, proj2.
  • Both projects depend on React, and definitions are installed for each through npm install @types/react
  • They compile independently.

image

The problem: I get a "duplicate identifier" error when compiling proj2:

  • when the compiler processes a proj2 file it resolves module 'react' to proj2's proj2/node_modules/@types/react. All good.
  • when the compiler processes a proj1 file it resolves 'react' to proj1/node_modules/@types/react which would be fine, and hits the duplicate identifier error

Simplified Repro:

  1. Unzip bugreport.zip
  2. cd bugreport/proj1
  3. ./node_modules/.bin/tsc
  4. cd ../proj2
  5. ./node_modules/.bin/tsc

Expected behavior: succeeds. All is good.

Actual behavior:

../proj1/node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.
index.ts(4,17): error TS2345: Argument of type 'typeof Proj1Comp' is not assignable to parameter of type 'ReactElement<{}>'.
  Property 'type' is missing in type 'typeof Proj1Comp'.
node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.

How are we supposed to handle these situations where a dependency brings its own type definitions for a shared library?

@mattyork mattyork changed the title Duplicate identifier error when dependent supplies its own type definitions for a shared library Duplicate identifier error when dependent project supplies its own type definitions for a shared library Sep 17, 2016
@RyanCavanaugh
Copy link
Member

I can't reproduce this using the latest compiler version. Looks like a duplicate of #9771?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Sep 19, 2016
@mattyork
Copy link
Author

I can still repro with the above repro steps. Here's a simpler repro:

Here are easier steps:

  1. Copy and paste the following into your shell
mkdir dup-id-bug
cd dup-id-bug
mkdir A B
cd A
npm install @types/react
echo "import 'react';" > a.ts
cd ../B
npm install typescript@2.0 @types/react
echo "import '../A/a';" > b.ts
./node_modules/.bin/tsc b.ts

Here's all my output with the errors at the bottom. Note the specific versions @types/react@0.14.35, typescript@2.0.2

Downloads >mkdir dup-id-bug
Downloads >cd dup-id-bug
dup-id-bug >mkdir A B
dup-id-bug >cd A
A >npm install @types/react
/Users/mattyork/Downloads/dup-id-bug/A
└── @types/react@0.14.35

npm WARN enoent ENOENT: no such file or directory, open '/Users/mattyork/Downloads/dup-id-bug/A/package.json'
npm WARN A No description
npm WARN A No repository field.
npm WARN A No README data
npm WARN A No license field.
A >echo "import 'react';" > a.ts
A >cd ../B
B >npm install typescript@2.0 @types/react
/Users/mattyork/Downloads/dup-id-bug/B
├── @types/react@0.14.35
└── typescript@2.0.2

npm WARN enoent ENOENT: no such file or directory, open '/Users/mattyork/Downloads/dup-id-bug/B/package.json'
npm WARN B No description
npm WARN B No repository field.
npm WARN B No README data
npm WARN B No license field.
B >echo "import '../A/a';" > b.ts
B >./node_modules/.bin/tsc b.ts
../A/node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.
node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.`

@mattyork
Copy link
Author

Ah, by "latest compiler version" you meant the nightlies, not version 2.0.2.

Looks like this was fixed on 8/26, which included merge #10354.

B >npm install typescript@2.1.0-dev.20160827 &>/dev/null && ./node_modules/.bin/tsc b.ts
B >npm install typescript@2.1.0-dev.20160826 &>/dev/null && ./node_modules/.bin/tsc b.ts
../A/node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.
node_modules/@types/react/index.d.ts(7,21): error TS2300: Duplicate identifier 'React'.

However, the bug still repros in version 2.0.2, which is the original milestone for the bug #9771.

Is it possible to get this backported to hit the official 2.0 release? Us folks at Tableau really want TypeScript 2 goodness, but we need this fix, and we're too conservative to be using nightly builds for our core modules.

@peanutgyz
Copy link

[default] Checking started in a separate process...
[default] /Users/gui/ng2-admin/node_modules/.0.1.27@@types/source-map/index.d.ts:7:21
Duplicate identifier 'sourceMap'.
[default] /Users/gui/ng2-admin/node_modules/@types/source-map/index.d.ts:7:21
Duplicate identifier 'sourceMap'.
[default] Checking finished with 2 errors

@RyanCavanaugh
Copy link
Member

2.0.5, which will the official 2.0 release, will have this fix

@mattyork
Copy link
Author

Just saw that 2.0.3 was released, but looks like the fix is not in there yet. :(. Will have to wait for 2.0.5

@mhegazy mhegazy added this to the TypeScript 2.0.5 milestone Sep 22, 2016
@mhegazy mhegazy added Bug A bug in TypeScript Fixed A PR has been merged for this issue and removed Needs More Info The issue still hasn't been fully clarified labels Sep 22, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 22, 2016

Fix should be in the 2.0.5

@mhegazy mhegazy closed this as completed Sep 22, 2016
@mattyork
Copy link
Author

Yep, looks like it's there:

> git branch -a --contains 0116abdcf2cc57174a839deb29e6c4419c576dfd | grep 2.0.5
  remotes/origin/release-2.0.5

@mattyork
Copy link
Author

mattyork commented Sep 22, 2016

Workaround: include the new path mapping option to explicitly state the .d.ts you want to use: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#path-mapping

For example, in this repro you would add this to B/tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "react": ["./node_modules/@types/react/index"]
    }
  }
}

Then run > ./node_modules/.bin/tsc

@mKlus
Copy link

mKlus commented Nov 16, 2016

I have similar issue with jquery and knockout.
I'm using TS 2.0.6 and still get the following errors:

node_modules@types\jquery\index.d.ts(623,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\jquery\index.d.ts(2872,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\jquery\index.d.ts(2873,5): error TS2375: Build:Duplicate number index signature.
node_modules@types\jquery\index.d.ts(3246,5): error TS2300: Build:Duplicate identifier 'export='.
node_modules@types\knockout\index.d.ts(8,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\knockout\index.d.ts(14,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\knockout\index.d.ts(18,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\knockout\index.d.ts(38,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\knockout\index.d.ts(160,5): error TS2374: Build:Duplicate string index signature.
node_modules@types\knockout\index.d.ts(682,2): error TS2300: Build:Duplicate identifier 'export='.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 16, 2016

@mKlus please file a new issue and give us more info to be able to diagnose the issue you are running into.

@mKlus
Copy link

mKlus commented Nov 16, 2016

@mhegazy I have created new issue #12286.

@NN---
Copy link

NN--- commented Oct 18, 2017

In case someone encounters and everything doesn't work.

I had to manually specify exclude and typeRoots in new tsconfig.json, not sure if it is a bug or intended behavior.

{
  "exclude": [
    "node_modules",
    "out"
  ],
  "compilerOptions": {
    "typeRoots": [ "./node_modules/@types" ]
  },
  "extends": "../library/tsconfig.json"
}

@hasantayyar
Copy link

I've fixed my problem with this workaround
#11916 (comment)

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 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

No branches or pull requests

7 participants