Typescript: Duplicate identifier error when dependent project supplies its own type definitions for a shared library

Created on 18 Sep 2016  ·  14Comments  ·  Source: microsoft/TypeScript

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?

Bug Fixed

Most helpful comment

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

All 14 comments

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

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 [email protected] @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/[email protected], [email protected]

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/[email protected]

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 [email protected] @types/react
/Users/mattyork/Downloads/dup-id-bug/B
├── @types/[email protected]
└── [email protected]

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'.`

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 [email protected] &>/dev/null && ./node_modules/.bin/tsc b.ts
B >npm install [email protected] &>/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.

[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

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

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

Fix should be in the 2.0.5

Yep, looks like it's there:

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

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

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='.

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

@mhegazy I have created new issue #12286.

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"
}

I've fixed my problem with this workaround
https://github.com/Microsoft/TypeScript/issues/11916#issuecomment-257130001

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  ·  3Comments

bgrieder picture bgrieder  ·  3Comments

jbondc picture jbondc  ·  3Comments

uber5001 picture uber5001  ·  3Comments

siddjain picture siddjain  ·  3Comments