microsoft / microsoft/TypeScript

Allow non-readonly properties to be used in aliased conditional expressions

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

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

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

説明

Suggestion

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.

🔍 Search Terms

aliased conditional expressions condition control flow readonly property

⭐ Suggestion

Some beta feedback. I was excited to try out TypeScript 4.4 because it provided control flow analysis of aliased conditions but was disappointed to learn that it only works for readonly properties as mentioned in the implementation PR. I get the justification; unfortunately, I don’t use readonly because I don’t think the weak immutability guarantees which the modifier provides are worth the syntax noise or annoying assignability errors.

📃 Motivating Example

I digress. Limiting control flow analysis to readonly properties makes code like the following not work, which I would argue is somewhat surprising.

interface A {
  prop: string | undefined;
}

declare const a: A;
// this works
if (a.prop !== undefined) {
	const b: string = a.prop;
	console.log(b);
}

const hasProp = a.prop !== undefined;
// this errors
if (hasProp) {
	const b: string = a.prop;
	console.log(b);
}

💻 Use Cases

I just want to write code which can take advantage of aliased property checks without having to use the TypeScript’s readonly modifier.

Maybe we could assume some basic things about JavaScript object properties for the sake of developer experience. For instance, we could assume that non-getter property test aliases are safe to use in control flow analysis so long as the property is not directly reassigned.

In any case, I think adding this caveat to blog posts and all marketing copy describing the feature would be wise, because I will likely not be the last person to be confused by this limitation, and many TypeScript features are only documented in release notes.

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

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

はじめの一歩

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

調査の方向性

実装ファイル、テスト、エントリーポイントは指定されていません。まず動機となる TypeScript の例と、リンクされている control-flow analysis の実装に関する議論を確認し、その後 non-readonly プロパティのエイリアスに対する安全なルールを決定してください。完了の条件は、生成される JavaScript を変更せずに例が正しく絞り込まれ、必要に応じて制限事項が文書化されていることです。

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

評価

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

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

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