microsoft / microsoft/TypeScript

Suggestion: use receiver's declared type arguments as defaults to its class constructor

Aperta
#7,782 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Effort: Difficult Help Wanted Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

I run into this relatively simple pattern a lot:

class GenericClass<T, U, V> {
    a: T;
    b: U;
    c: V;
}

let x: GenericClass<number, string, boolean>;

x = new GenericClass();
// Which produces the error:
// Error: GenericClass<{}, {}, {}> is not assignable to GenericClass<number, string, boolean>

Since x has already been defined with a particular set of type arguments, these arguments could be implicitly assumed as defaults to its type's constructor when invoked with it being the receiver (for simplicity I'm assuming here it is the exact constructor as declared e.g. not a derived class).

I understand there will be cases where that would be very difficult or even impossible to achieve:

let createInstance = () => new GenericClass()
x = createInstance();

but these are relatively rare. Most of the time it's just:

class Example {
    myMap: Map<string, Array<{n: number}>>;

    constructor() {
        this.myMap = new Map<string, Array<{n: number}>>(); // <-- Why repeat?
    }
}

Which instead could have been:

class Example {
    myMap: Map<string, Array<{n: number}>>;

    constructor() {
        this.myMap = new Map(); // <-- Better.. :)
    }
}
  • Any possible issues with this? perhaps ones that I'm not aware of or haven't thought of?
  • Potential name collisions? (though I'm not aware of any?)
  • Maybe just a bit too difficult to implement? perhaps revisit later? (though #5256 appears more difficult than this and seems to be in consideration?)
  • Or maybe this 'bends' the semantics of the constructor expression (new Class<T>()), or at least its default type arguments, in a way that's slightly unappealing from a "purist" point of view"? (though this doesn't seem like a "purist" language?). Perhaps some concept of "contextual baseline default type arguments" can be included as an integral part of the future proposal for default type arguments?

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

Non vengono indicati file, test o punti di ingresso del compilatore. Inizia esaminando gli esempi GenericClass e Map, quindi traccia il modo in cui gli argomenti di tipo del costruttore vengono dedotti dall’espressione ricevente. Il lavoro sarà considerato completo quando sarà stata definita una gestione chiara dei valori predefiniti contestuali, delle chiamate indirette, delle classi derivate e della semantica dei costruttori.

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
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.