microsoft / microsoft/TypeScript

TS2416 nonsensical fix suggestion (and possible false positive?) in a generic class implementing an interface verbatim

Offen
#59,377 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Variance Relationships Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

TS2416

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about covariance in method params
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.3#code/JYOwLgpgTgZghgYwgAgKoB4AqBGANMzAJgD5kBvAWAChlbkAHQgLgMIG5kB6T5KCAWwD2AN1ABzZGAAWwAM7JBUXgJHiC2BnChx+yGFEG7pKUJFiIUMYAA8I848mgGo1AL7Vqp6PCTIAkgBigoJYpJQ0dJCyYOh+RPgACo7WkCAAJvIYmPhxJMQAFGDM-vEMLAkAlCzCgsBpbNR0XDy0AHrICIb0wAA20MiyAK5iYnZg9oLIg7IoAAa5yakZJYSzyFLQEPgA7jIIUsj8cADWdsggkzMgM24eVAg9cLLyQSGYpMD89H38EODygWCoXIjUiYywhESiz+yyy+CIxAKRRYpXo5SqyBqdRBEVo3CaBMJROJyHaszgIAAnmsNnw9DYzg4nIpQXieO0EgZ6NAwJTkAByKJgfnIUCSSncgWvUIiuTnQRgZBPWTAMQgOAAIz6kkmDlkOhQ9C5PL5Yo1TxQvMl-MBb2I-IAdKzmrRMBKUPyIVCICkYZksPC8oViqj0cgALykLFpWXyC6K5Wq9Vay2690C2KlJI+pb+7IrRHBli5fBo5CVCNR2oxp24l10N3c+SCGCabS-Mzyfn0EUUtICntKumgTpfOBgYAp2tNfEExsehKx+UJ55JzXasBp61wguO52zwnzgU73L20VxhVK1dq9ep8XbgO7h0EbaTNLAGAwTbge9nbbAaRJBkOMDWSOQwHwDVBkVYw+S0FBBhAPhHkgNJpzoA8iSPG0iCXeMrxVG8Ux1X8BVyR0XzfD8vz4H8rT-ACDmkOV1V+MDokg6CgIgOC6UQ5DxwgNDnXcKhXCAA

💻 Code
interface U<T1, T2> {
    p2: T2; // removing this or removing T1 param from the interface fixes the error
}

interface IFoo<T> {
    test<IT2, P extends U<T, IT2>>(t2: IT2, p: P): void;
    //   ^ compiler suggests to use `IT2 extends IT2` here, which makes no sense
}

class Foo<T> implements IFoo<T> {
    test<T2, P extends U<T, T2>>(t2: T2, p: P): void {
    //                               ^ `any` here fixes the error
    // ^ Property 'test' in type 'Foo<T>' is not assignable to the same property in base type 'IFoo<T>'.
    //  Type '<T2, P extends U<T, T2>>(t2: T2, p: P) => void' is not assignable to type '<IT2, P extends U<T, IT2>>(t2: IT2, p: P) => void'.
    //    Types of parameters 'p' and 'p' are incompatible.
    //      Type 'P' is not assignable to type 'U<T, IT2>'.
    //        Type 'U<T, IT2>' is not assignable to type 'U<T, IT2>'. Two different types with this name exist, but they are unrelated.
    //          Type 'IT2' is not assignable to type 'IT2'. Two different types with this name exist, but they are unrelated.
    }
}
🙁 Actual behavior

Compiler suggests to modify the interface to extend it's IT2 generic parameter (IT2 extends IT2), which makes no sense (and is an invalid ts code), which suggests, that compiler itself is confused about what exactly is wrong with this implementation.

The interface is implemented verbatim.
The error message shows, that verbatim copies of inferred types are not the same type (mind, i changed the generic param names to check, if i assumed something wrongly about inferred generics in specific places, yet it's Type 'IT2' is not assignable to type 'IT2'. Two different types with this name exist, but they are unrelated.)
Something is wrong somewhere but error message only confuses you more.

🙂 Expected behavior

If it is a bug, it should not error on verbatim implementation of an interface.

If it isn't, it should print an intelligible error message, instead of "hey, 'warm' !== 'warm', 'cold' !== 'cold' and 'soft' !== 'soft', din't you know?", and it should not suggest nonsensical fixes, like "replace generic T with T extends T"

Additional information about the issue

It is possibly related to issue #30071 which have been fixed in v4.3.5, apparently (playground)
Notice the similarities (bug is present in 4.2.3), removing then generic param T from interface or using : any for the parameter using T2 makes the error go away (but there is no nonsensical fix suggestion in this case)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die generische Schnittstelle und Klassenimplementierung im verlinkten TypeScript Playground und vergleiche anschließend das Verhalten mit dem referenzierten Issue #30071 sowie den Versionen 4.2.3 und 4.3.5. Ermittle, ob die unveränderte Implementierung eine Typprüfung bestehen sollte oder ob die Diagnose und der vorgeschlagene Fix korrigiert werden müssen; abgeschlossen ist die Aufgabe, wenn der gemeldete Fall das korrekte Verhalten und eine verständliche Meldung aufweist.

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
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.