Repository: TypeScript
Issue: error messages for duplicate declarations do not point users to root cause
Labels: Suggestion,Experience Enhancement

# Bug Report

TL;DR: "error message could be better"

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->

TS2374 duplicate global declaration

### 🕗 Version & Regression Information

4.6.3

(but this has likely existed for a long time)

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- This is a crash
- This changed between versions ______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about duplicate types
- I was unable to test this on prior versions because _______

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?#code/PTAEFkE9QYwewCYFNQHM5IM6gBZIE5JA)

I'm not sure how to use the playground to reproduce this specific problem.

### 💻 Code

No code, but a dependency tree.

1. I had a direct dependency of `@wdio/types` version _a_ which declares global var _x_
2. I had transitive dependency of `@wdio/types` at version _b_ which _also_ declares global var _x_ 
3. `tsc` reported a bunch of errors about duplicates

The fix was to make sure these versions are the same. Note that the actual _decarations themselves_ seemed to be identical, but it's just that they exist twice.  This may (?) happen in other situations not involving globals, but this is the one I encountered.

### 🙁 Actual behavior

Example errors:

```
node_modules/@wdio/types/build/index.d.ts:23:13 - error TS2374: Duplicate index signature for type 'string'.

23             [key: string]: any;
               ~~~~~~~~~~~~~~~~~~~

node_modules/@wdio/types/build/index.d.ts:35:19 - error TS2320: Interface 'Config' cannot simultaneously extend types 'Testrunner' and 'Testrunner'.
  Named property 'capabilities' of types 'Testrunner' and 'Testrunner' are not identical.

35         interface Config extends Options.Testrunner {

packages/execute-driver-plugin/node_modules/@wdio/types/build/index.d.ts:43:14 - error TS2300: Duplicate identifier 'Capabilities'.

43         type Capabilities = Capabilities.Capabilities;
                ~~~~~~~~~~~~

  node_modules/@wdio/types/build/index.d.ts:43:14
    43         type Capabilities = Capabilities.Capabilities;
                    ~~~~~~~~~~~~
    'Capabilities' was also declared here.
```

### 🙂 Expected behavior

The above errors are valid and expected.  However, it's difficult to understand what's _actually happening_ here based on _just_ these error messages.  TS2300 will point you in the right direction--though by no means will it tell you the root cause--but TS2374 is unhelpful.

Here are some possibly unworkable ideas:

- Improve the error messaging in TS2374 to point to where the "other" index signature was defined, like in the TS2300 message 
- A new error message _in addition_ to the above; e.g., "Hey! You have two versions of this package installed and they are both declaring the exact same globals! You should probably just pick one!"  
- The fix is neither in the code _nor_ is it in `tsconfig.json`.  The fix is in `node_modules`, and that means the developer must edit a `package.json` somewhere.  If `tsc` was able to deduce this and tell the user plainly, that'd be very helpful.
- Provide information on where to look to find out why these two declarations are being pulled in, in what order, what's using them, etc.
- I do understand that the errors are likely terse _on purpose_; maybe it's better to add more exposition to the documentation site and/or link to it from the error message