microsoft / microsoft/TypeScript

Allow inference of class generics when declaring an interface

Offen
#32,044 1 Kommentar 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

interface, inference, extending class

Suggestion

I'd like to be able to strongly type an interface such that when it is used on a class that has a generic I can infer the type of that generic to then use in the declaration of the interface.

Use Cases

I want to use this within a library that exposes both an abstract class and an optional interface that developers can opt-in to use. The abstract class has one generic parameter, and in order to correctly type the interface without duplicate generic declaration I want to be able to infer the abstract class' generic type.

Currently the inheriting class declarations must duplicate their generic type declaration.

This issue is similar to https://github.com/microsoft/TypeScript/issues/26242 I think, but I don't see how the resolution of that issue will fix this one.

Examples

abstract class Base<T> {
    // implementation omitted for brevity
}

// ⬇️this is the proposed new syntax ⬇️
interface Remap<T> extends Base<infer U> {
    remap(input: U): T;
}

class Foo extends Base<string> implements Remap<number> {

    public remap(input: string): number {
        return input;
    }

}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Im Issue sind keine Quelldateien, Tests oder Compiler-Einstiegspunkte identifiziert. Beginne damit, die vorgeschlagene TypeScript-Syntax und das zugehörige Issue #26242 zu lesen, und verfolge anschließend, wie generische Vererbung und Interface-Deklarationen derzeit geprüft werden. Als abgeschlossen gilt die Aufgabe, wenn das Team das beabsichtigte Inferenzverhalten definiert und implementiert hat und Abdeckung für das gezeigte Beispiel sowie dessen Randfälle vorhanden ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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