microsoft / microsoft/TypeScript

Generic of abstract class should be inferrable from abstract property implemented in subclass

Offen
#39,180 11 Kommentare 9 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

  • generic
  • abstract class
  • abstract property
  • type inference

Suggestion, Use Case and Examples

As of typescript 3.9.3, if I write a code like the following:

abstract class SomeMapper<T extends string> {
    private someMap = new Map<number, T>();
    protected abstract readonly rndText: T;

    getSome(num: number) {
        return this.someMap.get(num)!;
    }
}

function rndGender(): 'male' | 'female' {
    return Math.random() > 0.5 ? 'male' : 'female';
}

class Gender extends SomeMapper<'male' | 'female' /* need to provide this explicitly */> {
    readonly rndText = rndGender();

    constructor() {
        super();
    }
}

const gen = new Gender();
const some = gen.getSome(1);

I have to make sure I always provide the generic to the SomeMapper class, and if I ever update the type of rndText in Gender class, I'll have to go and update the generic as well every time. The only way as of now to make this kinda auto-infer the generic, is to write it like this:

class Gender extends SomeMapper<Gender['rndText']> {
    readonly rndText = rndGender();

    constructor() {
        super();
    }
}

However this is not the most elegant way and can get very dirty with complicated type (for example check this), and typescript should be able to do this on its own, similar to what it does with generics in functions.

So the ideal behaviour should be simply like this:

// Below line should not error: Generic type 'SomeMapper<T>' requires 1 type argument(s).
// Should auto infer the generic from property rndText
class Gender extends SomeMapper {
    readonly rndText = rndGender();

    constructor() {
        super();
    }
}

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

Beginne mit der TypeScript-Reproduktion im Issue und vergleiche die explizite generische Form mit der vorgeschlagenen Form class Gender extends SomeMapper. Als erledigt gilt die Aufgabe, wenn die vorgeschlagene Deklaration eine Typprüfung besteht und den Generic aus der implementierten abstrakten Eigenschaft ableitet, ohne ein explizites Typargument zu erfordern.

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
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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