microsoft / microsoft/TypeScript
Allow declaring properties inside class constructors
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
It's a little clunky setting up properties in classes right now.
Here's what I have to do at the moment to set a property value in the constructor:
class Animal {
public interruptedSound: string;
constructor(public sound: string) {
this.interruptedSound = sound.substring(0, 3);
}
}
let dog = new Animal("woof");
console.log(dog.sound); // woof
console.log(dog.interruptedSound); // woo
Notice that interruptedSound's setup is split over two lines, and I have to declare it as a string even though it could be inferred from the fact that sound.substring returns a string.
What I'd like to be able to do:
class Animal {
constructor(public sound: string) {
this.interruptedSound = sound.substring(0, 3); // now a detectable property of type string
}
}
let dog = new Animal("woof");
console.log(dog.sound); // woof
console.log(dog.interruptedSound); // woo
This would avoid having to declare all properties at the top before being able to use them in constructors, saving a lot of boilerplate on complex classes. F# already does something similar to make setting up types much quicker and simpler. There wouldn't be any JavaScript compatibility issues, because JavaScript already works this way.
In cases where the property is declared separately, give precedence to the declaration.
The private, public or readonly keywords could even be used before the assignment to modify the property, e.g.
class Animal {
constructor(public sound: string) {
private readonly this.interruptedSound = sound.substring(0, 3);
}
changeSounds() {
this.sound = "meow";
this.interruptedSound = "meo"; // error, interruptedSound is readonly
}
}
let dog = new Animal("woof");
console.log(dog.sound); // woof
console.log(dog.interruptedSound); // error, interruptedSound is private
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
In der Issue werden keine Dateien, Tests oder Compiler-Einstiegspunkte genannt. Beginne mit der Prüfung der vorgeschlagenen Beispiele für Zuweisungen im Konstruktor und der bestehenden Behandlung von Klasseneigenschaften und Konstruktor-Parametereigenschaften; als abgeschlossen gilt die Unterstützung der angeforderten Syntax mit abgeleiteten Typen sowie dem angegebenen Deklarations-, Sichtbarkeits- und readonly-Verhalten.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100