microsoft / microsoft/TypeScript
Suggestion: readonly method
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Problem:
A method (function written with the ES6 method syntax) can currently be replaced with another function by a simple assignment.
As an example consider the following snippet of code:
class Person {
constructor(...) {...}
displayName (): string {...}
}
const x: Person = new Person(...)
x.displayName = function(this: Person): string {...} // Assignment
Because the readonly modifier is not usable for methods, this is not possible to prevent this kind of assignments.
Proposal:
A method is always readonly.
The following codes are identical:
class Person {
displayName (): string {...}
}
class Person {
readonly displayName: (this: Person) => string = function () {...}
}
Compatibility:
This is a breaking change. However, method syntax is recent and mostly used in classes. Codes which assign a function to a method are certainly rare.
Temporary workaround:
Do not use method syntax in your classes and interfaces. Note hat this leads to very verbose codes.
If you use an interface, the verbosity is acceptable. However you get also strict variance.
interface PersonI {
readonly displayName: (this: Person) => string
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es ist keine Quelldatei und kein Test benannt. Beginne damit, die Beispiele für Klassenmethoden und readonly-Eigenschaften im Issue zu vergleichen, und verfolge dann, wo TypeScript Zuweisungen zu Methoden prüft. Als abgeschlossen gilt die Arbeit, wenn das Verhalten von readonly-method entschieden ist, die Kompatibilität für die vorgeschlagene Breaking Change behandelt wird und Tests vorhanden sind, die die Ablehnung von Zuweisungen abdecken.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, 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