microsoft / microsoft/TypeScript

Distinct type specification for public field members

オープン
#54,829 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

Right now for a field of a class we can use the modifiers public, protected & private to control visibility outside of the class, and also readonly to control whether reassignment is permitted. For encapsulation purposes it is nice to have a field that is publicly accessible however not necessarily modifiable, for example with #-prefix'd fields we can do:

class Holder {
  #holds = new Set<number>();
  
  get holds(): ReadonlySet<number> {
    return this.#holds;
  }
}

Within the class one can use this.#holds to get a Set, or externally this.holds to get ReadonlySet. It would be nice if the above in-fact had a shorthand that didn't require an accessor specification, e.g.

class Holder {
  public(ReadonlySet<number>) holds = new Set<number>();
}

Which is functionally identical to the above (including in terms of inferred typing). The definition would be that:

The public modifier, if with parenthesis-form, is permitted in addition to the private/protected modifiers (both optional) and specifies an alternative type to infer in a public context. If specified without other modifiers the field is presumed private.

🔍 Search Terms

private, modifier, public, access, readonly, fields, members

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

📃 Motivating Example

In order to detect bugs early narrow typing is often desirable to limit mutability of class fields, for example:

class ShardManager {
  held = new Set<number>();
  private lock = new DistributedLock();
 }
const shards = new ShardManger();

For brevity, ease of access, and succinct code, it is quite nice to be able to do shards.held in public code to get the list of currently held shards. However doing so also exposes risk, especially in libraries, as doing something simple like shards.held.add(5) could have unintended consequences by not respecting the assumptions the rest of the class makes (e.g. changes only occur under the lock).

To support this case we support a distinct type for public field access.

💻 Use Cases

Work-around:

class ShardManager {
  private myHeld = new Set<number>(); // Or #held
  private lock = new DistributedLock();
  get held(): ReadonlySet<number> {
    return this.myHeld;
  }
}

Is more lines of code, more verbose, and disconnects the access control from the field definition.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

この issue ではファイル、テスト、エントリポイントが指定されていません。まず、提案されている括弧付きの public modifier と、private、protected、readonly、推論されるフィールド型との相互作用を確認してください。完了とするには、設計を確定し、それに対応する型チェックの動作を定める必要があります。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。