microsoft / microsoft/TypeScript

Request: Infer generics

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

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

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

説明

The request is to let infer support generics.

Background:

I recently published a recursive Pipe and Compose. It offers some key advantages, including the preservation of variable names.

https://github.com/babakness/pipe-and-compose-types

Its shortcoming is in the case of generics. This is because infer does not work well with generics. Example

/**
 * Extracts function arguments
 */
export type ExtractFunctionArguments < Fn > = Fn extends  ( ...args: infer P ) => any  ? P : never

/**
 * Extracts function return values
 */
export type ExtractFunctionReturnValue<Fn> = Fn extends  ( ...args: any[] ) => infer P  ? P : never

type Foo = ExtractFunctionArguments< <A>(a:A, b:number) => A >
// Foo has type [ {} , number ]

TypeScript 3.4 improved how pipe and compose functions work with generics with higher order type inference from generic functions.

Common implementations of pipe and compose rely on parameter overloading. However, even here, there is an issue with variadic functions. See example in this issue:

#30727

The broader issue with the parameter overloading version of pipe and compose is that parameter names are lost, decreasing code clarity. The recursive version, relying on infer does maintain parameter names.

Providing support generics with infer will present a major productivity bump and will nearly complete the community's need for the common place pipe and compose in FP patterns without compromise.

The only outlier is a small situation with parameter names on variadic functions of a minimum arity of zero as opposed to at least one. Can provide examples if needed since it is a separate issue.

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

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

はじめの一歩

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

調査の方向性

まず、issue にあるジェネリック関数の例を再現し、infer が関数の引数と戻り値をどのように扱うかに焦点を当てます。TypeScript 3.4 の高階ジェネリック推論のコンテキストを読み、issue #30727 の可変長引数関数に関する議論と比較します。infer を通じてジェネリック型が保持され、意図したパラメーター情報が失われなければ完了です。

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

評価

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

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

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