microsoft / microsoft/TypeScript

Support conditional types for Generator T / TNext / TReturn

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

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

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

説明

🔍 Search Terms

"Generator", "Async Generator", "conditional types", "const" "inference"

✅ Viability Checklist
⭐ Suggestion

If a generator function is specifically typed to have a conditional relationship between T and TNext then type inference should allow for a type narrowed result according to that conditional type.

📃 Motivating Example

Typescript would be able to support complex type state management (ala redux saga), coroutines (e.g. co) and the interpreter pattern / free monad/applicative.

In the past this was not possible, but given typescript now has as const, infer and conditional types, we should be able to support typed yield result inference in key circumstances.

Something like this would be possible:

type Yielded<T> 
  T extends Promise<infer U>
   ? U // awaited
   : T  // return the original yielded value
   
function co<T>( fn: () => Generator<T, T, Yielded<T>> ) : Promise<T> {...}
💻 Use Cases
  1. What do you want to use this for?

Complex state management and side effects.

  1. What shortcomings exist with current approaches?

For generator functions yielded values are always typed as any. For async generators yielded values are a union of all possible results.

  1. What workarounds are you using in the meantime?

Manually casting

doSomething(function * f(){
    let value = (yield someStream) as UnwrapStream<typeof someStream>
    let another = (yield somePromise) as UnwrapPromise<typeof somePromise>
})

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

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

はじめの一歩

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

調査の方向性

動機となるジェネレーターの例から始め、T、TNext、TReturn の間で要求されている条件付きの関係を、ジェネレーターおよび async ジェネレーターについて現在示されている動作と比較します。条件付き推論が説明されている型付き yield 結果をサポートし、出力される JavaScript を変更せず、動機となるケースをカバーできれば完了です。

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

評価

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

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

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