microsoft / microsoft/TypeScript
Allow accessors to support inline generics for this
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
getter, accessor, this, generic
Suggestion
Currently accessor declarations do not support generics at the declaration site. The error returned is An accessor cannot have type parameters.ts(1094). Currently the accessor can implement generic declaration via the enclosing class. The goal would be to allow for the accessor to support a generic this at the declaration site.
Use Cases
The use case is to allow for more expressive setter/getter declarations, in line with how method declarations currently work.
Examples
Suggested Pattern
Below is an example of defining the constraints of this at the accessor site, to determine if an inherited accessor method is valid by the shape of the subclass.
class Base {
get left<T extends { _left: string}>(this: T) {
return this._left;
}
get right<T extends { _right: string}>(this: T) {
return this._right;
}
}
...
class Left extends Base {
_left: string = '';
}
new Left().left; // OK
new Left().right; // Errors
...
class Right extends Base {
_right: string = '';
}
new Right().right; // OK
new Right().left; // Errors
...
class Both extends Base {
_right: string = '';
_left: string = '';
}
new Both().right; // OK
new Both().left; // OK
This pattern can currently be emulated by converting the accessors into standard methods
class Base {
getLeft<T extends { _left: string}>(this: T) {
return this._left;
}
getRight<T extends { _right: string}>(this: T) {
return this._right;
}
}
...
class Left extends Base {
_left: string = '';
}
new Left().getLeft(); // OK
new Left().getRight(); // Errors
...
class Right extends Base {
_right: string = '';
}
new Right().getRight(); // OK
new Right().getLeft(); // Errors
...
class Both extends Base {
_right: string = '';
_left: string = '';
}
new Both().getRight(); // OK
new Both().getLeft(); // OK
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia confrontando la sintassi dell’accessor richiesta e i vincoli di this con il modello esistente dei metodi generici descritto nell’issue. Esamina come TypeScript segnala attualmente An accessor cannot have type parameters.ts(1094) e come dovrebbero essere verificati gli esempi di accessor ereditati. Il lavoro è completato quando le dichiarazioni di accessor proposte vengono verificate dal sistema dei tipi come previsto, gli accessi ereditati non validi continuano a produrre errori e il comportamento esistente dell’output JavaScript rimane invariato.
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
- 28/100