microsoft / microsoft/TypeScript
Ability to replace return type of function type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
- replace return type
- parameters generic
Suggestion
I would like to be able to replace just the return type of a function with a utility type, like WithReturn<F, R>.
I'm not 100% sure this is needed, but it seems like using (...args: Parameters<F>) => R is not sufficient because any generics in F are lost.
Use Cases
My use case is a utility function that captures the arguments to a function to allow it to be called later.
Examples
export const defer = <F extends (...args: any) => any>(f: F) => ((
...args: Parameters<F>
) => ({
function: f,
args,
})) /* as any as F */;
const map = <T, R>(array: Array<T>, f: (i: T) => R) => array.map(f);
const deferredMap = defer(map);
let r = deferredMap([1], (i: number) => 2); // error now: Type 'unknown' is not assignable to type 'number'.(2345)
r.args; // error if defer returns F
Right now there's an error because the generics to map() are lost when using Parameters.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- 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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground でジェネリック消失の挙動を再現することから始め、Parameters と提案されている WithReturn<F, R> の挙動に焦点を当てます。関数型のジェネリックパラメーターを保持したまま戻り値の型だけを置き換え、JavaScript のランタイム出力を変更しない Utility が実現できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100