microsoft / microsoft/TypeScript
Allow declaring properties inside class constructors
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue にはファイル、テスト、コンパイラのエントリポイントは記載されていません。まず、提案されているコンストラクター代入の例と、クラスプロパティおよびコンストラクターパラメータプロパティの既存の扱いを確認してください。完了とは、推論された型と、記載された宣言、可視性、readonly の動作を伴う、要求された構文をサポートすることを意味します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100