Repository: TypeScript
Issue: Module augmentation allows non-identical type parameters as long as there are defaults, but doesn't pass them
Labels: Bug,Help Wanted

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

- module augmentation
- callback generic type

### 🕗 Version & Regression Information
Currently using `typescript` 5.1.6, but AFAIK this behavior was the same in previous versions. This also occurs on the Nightly.
<!-- 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 the behavior in every version I tried, and I reviewed the FAQ for entries about generics (and the rest of the FAQ)

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior.
  This should have the same code as the code snippet below, and use whichever settings are relevant to your report.

  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?ts=5.2.0-dev.20230714#code/JYOwLgpgTgZghgYwgAgMIAs6jgIwDYQA8AygK44BWECYyAvMnCAJ4B8yA3gLABQy-yAO7Aw6UAAoYIAFzJxABygQAbmUrUwstVRoBKeuwxYQuAiXI6wrXbKPZ8RbRtYBuXgF9evUJFiIUdiYO5uo09IwsADTIAKIAHmBQcAAqzPIoDKQgACYQMKAQ2ezcfALCohJSsgpKqhYaWvU00RAJSbLxiSlpEPp0hpj2Zk401raDQcNNYNGdSanprh5ePD7Q8EhoE6YQADLAANaO0+FMzLNt3enhWbn5IIXFvAJCImIgkjJyiiojmsh-PoDYw7EKWMZbEHBP5LUr8crvT7VH51UL-P4tS4dS4LXoGSFDY5oiGBUEY2I4nqwzw8XhgHoEyYQADqb2I1AA9jlceFScEAM6JUAAc2igqgIth9OuzKgXOFvO2DlZonZCC52VxAG0AOQI0A6gC6bh40oCHKgSjCDD5BH2R0I4pFYqFIGFrF1+pARpcQA)

### 💻 Code

<!-- Please post the relevant code sample here as well. This code and the Playground code should be the same, do not use separate examples -->
```ts
interface Chainable<Subject = any> {
    within(fn: (prevSubject: Subject) => Chainable<Subject>): Chainable<Subject>;
}

interface Chainable<Subject = any, ExtraType = undefined> {
    within(fn: (prevSubject: Subject, extra: ExtraType) => Chainable<Subject>): Chainable<Subject, ExtraType>;
}

interface ChainableLike<Subject = any, ExtraType = undefined> {
    within(fn: (prevSubject: Subject) => Chainable<Subject>): Chainable<Subject>;
    within(fn: (prevSubject: Subject, extra: ExtraType) => Chainable<Subject>): Chainable<Subject, ExtraType>;
}

type ChainableWithSecondType = Chainable<string, string>;
type Wrong = ChainableWithSecondType['within'];
type Correct = ChainableLike<string, string>['within'];
```

### 🙁 Actual behavior
If you omit the default on the added generic ExtraType, you get an error: `All declarations of 'Chainable' must have identical type parameters.` But when there's a default, it allows the type parameters to be different, but doesn't properly infer types.
![image](https://github.com/microsoft/TypeScript/assets/59899746/15eb671f-e717-4d12-8e71-a3016f6b8fa0)

Typescript does not produce any errors, but when you mouseover `Wrong`, it shows the type of `extra` to be `ExtraType`.
![image](https://github.com/microsoft/TypeScript/assets/59899746/56a274f2-d51f-4843-891e-e758ecabff60)


### 🙂 Expected behavior

Either TS should completely ban adding an optional extra type parameter, or it should properly infer type (i.e. mousing over Wrong should produce the same as mousing over Correct)

![image](https://github.com/microsoft/TypeScript/assets/59899746/22e81bf5-f5ed-4b1b-a4f2-52386a5f978d)