microsoft / microsoft/TypeScript
Module augmentation allows non-identical type parameters as long as there are defaults, but doesn't pass them
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Bug Report
🔎 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.
- 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
Playground link with relevant code
💻 Code
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.
Typescript does not produce any errors, but when you mouseover Wrong, it shows the type of extra to be ExtraType.
🙂 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)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Verhalten mithilfe des verknüpften TypeScript Playground und vergleiche die Typen Wrong und Correct im bereitgestellten Beispiel zur Modul-Augmentierung. Verfolge, wie der Compiler unterschiedliche Listen generischer Parameter prüft, wenn hinzugefügte Parameter Standardwerte haben, füge dann Testabdeckung für das gemeldete Verhalten hinzu und überprüfe, dass optionale zusätzliche Parameter entweder abgelehnt oder konsistent abgeleitet werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100