microsoft / microsoft/TypeScript

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

Aperta
#39,180 11 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione in TypeScript nell’issue e confronta la forma generica esplicita con la forma proposta class Gender extends SomeMapper. Il lavoro è completato quando la dichiarazione proposta supera il controllo dei tipi e inferisce il generico dalla proprietà astratta implementata, senza richiedere un argomento di tipo esplicito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.