microsoft / microsoft/TypeScript
Allow declaring properties inside class constructors
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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
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
Nell’issue non sono indicati file, test o punti di ingresso del compilatore. Inizia esaminando gli esempi proposti di assegnazione nel costruttore e la gestione esistente delle proprietà di classe e delle proprietà dei parametri del costruttore; il lavoro sarà considerato completato quando sarà supportata la sintassi richiesta con tipi inferiti e il comportamento dichiarato per dichiarazione, visibilità e readonly.
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
- 30/100