microsoft / microsoft/TypeScript
Allow inline type predicates
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
From #5731
if (<foo is Element>(foo.nodeType === 1)) {
// Assume foo is Element here
}
A proposal from @sandersn: https://github.com/Microsoft/TypeScript/issues/5731#issuecomment-162586789
Type Predicate Expressions
A type predicate expression allows you to narrow a union type with a single expression. The syntax is
<variableistype>boolean-expressionWhen the expression is used in a conditional context, the true branch of the conditional narrows variable to type, while the false branch narrows variable by removing type.
The type predicate expression is of type Boolean, with apparent type of type predicate. The right hand side must be an expression of type Boolean. The left hand side's variable must be a name bound in the current scope. The left hand side's type must be a name bound in the current scope.
Open questions
- How does variable capture work? Can a type predicate expression be returned from a function and used to narrow a variable that's no longer in scope? This could be defined to cause an error, but that restriction is neither obvious nor easy to use.
- Why reuse the type assertion syntax? The type of the right expression is checked to be Boolean, unlike assertions, and the resulting type is still Boolean, also unlike assertions.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず issue #5731 からリンクされている提案と、こちらの Type Predicate Expressions セクションを読んでください。先に進む前に、変数のキャプチャ、構文、Boolean チェックについて列挙されている質問を解決してください。この例について inline predicate 構文と、その true ブランチおよび false ブランチでの narrowing の動作が仕様化され、サポートされていれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100