microsoft / microsoft/TypeScript

Way to detect whether a callable has type parameters

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

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

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

説明

🔍 Search Terms

conditional type detect callable type parameter

✅ Viability Checklist
⭐ Suggestion

I'm looking for some way to detect if a callable type is generic and has type parameters.

LLMs are hallucinating an answer that would be nice if it worked:

type HasTypeParameter<T extends (...args: any[]) => any> =
  T extends <G>(...args: any[]) => any ? true : false;
📃 Motivating Example

I have a utility type that wants to map the return value of a callable. However because as far as I know it's not possible to map a type-parametrized callable keeping its parametrization, we prefer to bail out in this case and maintain the original type.

Roughly it looks like

type MappedCallable<T extends Callable> = HasTypeParameter<T> extends true 
  ? T
  : (...args: Parameters<T>) => MappedResult<ReturnType<T>>;
💻 Use Cases

I can achieve this detection in some case where the type of the callable is incompatible with a mapped callable were its type parameters inferred as any.

type LimitedHasTypeArgument<T extends (...args: any[]) => any> =
  ((...args: Parameters<T>) => ReturnType<T>) extends T ? false : true

However this doesn't work in some more complex cases (still trying to get a minimal repro).

I cannot think of other workarounds, which is forcing us to forego the result type mapping completely.

Of course having a way to keep the callable type-parametrization in the result mapping would be even better.

The specific use case is to map all callable in the result, including object methods, to callables returning a promise, while maintaining other parts of their signature.

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

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

はじめの一歩

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

調査の方向性

この issue ではソースファイル、テスト、実装のエントリーポイントが指定されておらず、最小限の再現ケースはまだ開発中だとされています。まず、ジェネリック callable の例をコンパイラテストに縮小してください。完了とみなすには、callable 型パラメーターを検出または保持する信頼できる方法、あるいは文書化された解決策が必要です。

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

評価

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

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

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