microsoft / microsoft/TypeScript

Feature request: typed yield expression

オープン
#36,967 コメント 7 件 リアクション 41 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

generator, iterator, yield, type inference, co, redux-saga

Related Issues

#32523

Suggestion

Allow passing of yield operator signature to generator functions

declare function fetchUser(): Promise<User>;
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
  const user = yield fetchUser(); // user type inferred to User based on yield param
  // impl
}

where yield inside params list is a pseudo-parameter similar to this.

Use Cases

Discussion of use cases is given in #32523.

My intended use is case is emulating do notation for monads with generator functions.
This implies I would normally have some generator runner that does composition defined before writing generator function, so type of expression at which generator resumes is trivially inferrable based on yielded value.

Discussion

The hard thing in typing yield expressions in is that resumed type does not depend only on passed parameters and generator type, but also on context in which the generator is run.

There is a precedent in TypeScript with similar situation -- this parameter in functions.
The situation is analogous -- what this behaves like depends on which object function is member of.

The proposal is to allow declaring contextual info about yield inside generator functions, by introducing appropriate pseudo-param.

I could see this working in two ways:

  • yield param gets erased early, it does not affect generator function signature,
    it serves merely as a contract for type-checking / inferrence inside body
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
  // impl
}
typeof asyncAction == () => Generator<...>
  • yield param is preserved as type info like this param.
    then it is always present on types of shape (yield: ...) => Generator<> and defaults to (a: any) => any.
function asyncAction*(yield: <R>(p: Promise<R>) => R) {
  // impl
}
typeof asyncAction == (yield: <R>(p: Promise<R>) => R) => Generator<...>

Additionaly

  1. signature of yield must extend (a: any) => any and defaults to it
function asyncAction*(yield: number) {
  // impl
}
// Error: signature must be of shape (any => any)
  1. yielded values inside generator function body must be assignable to argument of yield signature
function asyncAction*(yield: (s: string) => void) {
  yield 7; // Error: number is not assignable to string
}
  1. this parameter has no effect on Generator<> return type, whether declared or
    other than obvious coherence guaranteed by (2)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    thats an opt-in feature
  • This wouldn't change the runtime behavior of existing JavaScript code
    type level feature, doesn't affect runtime behavior
  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

まず、関連する issue #32523 と、提案にある generator、iterator、yield の例を読んでください。擬似パラメーターに対する2つの提案された扱いと、その型付け規則を比較してください。完了の条件は、yield される値、再開時の型、関数シグネチャ、既存の Generator の動作を網羅する合意済みの設計です。

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

評価

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

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

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