Repository: TypeScript
Issue: Error Messages Related to Nested Identical Generic Names Could Be More Explicit
Labels: Suggestion,Needs Proposal,Domain: Error Messages,Experience Enhancement

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

identical generics
overlapping generics
nested generics

<!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback. -->

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines (please notify me if my checklist is incorrect):

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->

Modify generic-related error messages, that involve 2 or more nested, identically named generic labels, to be more explicit.
e.g. Append `"(which is a different type from the first 'T')"` (or something similar) to the original message.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

The following error message was caused by a function nested within a function using the same generic label (as well as some erroneous code.) I suggest we modify generic-related error messages where there are 2 or more nested generic labels that have the same name. A more explicit message would help nudge the developer towards the real error source.

![image](https://user-images.githubusercontent.com/39649820/105214831-3091a380-5b59-11eb-9575-3d86f0fcc411.png)

Here is a snippet of the actual source code:

```ts
export function useForm<T extends FormFieldData>() {
  // ...
  function<T extends FormFieldData>(input: FormFieldKeys<T>) {
    // ...
  }
}
```

The solution in this case was removing the unnecessary nested generic and using the `useForm` scoped generic:

```ts
export function useForm<T extends FormFieldData>() {
  // ...
  function (input: FormFieldKeys<T>) {
    // ...
  }
}
```

<!--
## 💻 Use Cases
-->

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->

@DanielRosenwasser @sandersn