microsoft / microsoft/TypeScript

Allow inline type predicates

オープン
#6,474 コメント 8 件 リアクション 27 件 担当者 0 名 GitHub で見る

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

Needs Proposal Suggestion
主要言語
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

<variable is type> boolean-expression

When 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
  1. 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.
  2. 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.

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

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

はじめの一歩

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

調査の方向性

まず issue #5731 からリンクされている提案と、こちらの Type Predicate Expressions セクションを読んでください。先に進む前に、変数のキャプチャ、構文、Boolean チェックについて列挙されている質問を解決してください。この例について inline predicate 構文と、その true ブランチおよび false ブランチでの narrowing の動作が仕様化され、サポートされていれば完了です。

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

評価

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

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

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