microsoft / microsoft/TypeScript
`ReturnType` and `InstanceType` don't work for functions/constructors with `never` in their parameters
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
ReturnType InstanceType any never
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
ReturnTypeandInstanceType.
⏯ Playground Link
💻 Code
type _1 = ReturnType<(...args: never) => 'foo'>
// ^? type _1 = any
type _2 = InstanceType<new (...args: never) => 'foo'>
// ^? type _2 = any
🙁 Actual behavior
ReturnType and InstanceType always evaluate to any when given a function/constructor whose parameters are ...args: never (or ...args: never[], a: never, b: string, etc).
🙂 Expected behavior
ReturnType and InstanceType should return the actual return/instance type.
Additional information about the issue
Changing the parameter constraint in the conditions of these types from any to never seems to fix the issue:
type ReturnType<T extends (...args: any) => any> = T extends (...args: never) => infer R ? R : any
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: never) => infer R ? R : any
I don't think this will cause any regressions, and did some spot-checking to validate that, but haven't run the entire test suite or anything like that yet.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンク先の TypeScript Playground にある 2 つの例を再現し、次に ReturnType と InstanceType の宣言を見つけて、それらの条件付きパラメーター制約を提案されている never 形式と比較してください。never パラメーターを持つ関数とコンストラクターのカバレッジを追加し、推論された型が any ではなく 'foo' になることを確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 42/100