microsoft / microsoft/TypeScript

Adding an overload to .filter breaks specific inference with nested functions

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

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

Bug Domain: check: Type Inference Help Wanted
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

"filter inference overload", "filter boolean"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about inferred types
⏯ Playground Link

https://tsplay.dev/wQbrvN

💻 Code
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n
    ? never
    : T;

// Comment this out
interface Array<T> { filter(predicate: BooleanConstructor, thisArg?: any): NonFalsy<T>[]; }

const id = <T,>() => (t: T) => !!t;

['foo', 'bar'].filter(id())
//                    ^?
🙁 Actual behavior

Nested function inference works (type parameter is string) without the additional overload, and fails (type parameter is unknown) with the overload (even though the added overload isn't the one in use).

🙂 Expected behavior

Nested function inference should work regardless of whether an overload is added to .filter or not.

Additional information about the issue

Adding the overload is described in https://github.com/microsoft/TypeScript/issues/50387 and made popular by ts-reset.
It's possible the overload order may matter in this instance, but difficult to test when the main overloads are coming from lib definitions.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground で動作を再現し、追加された Array.filter BooleanConstructor オーバーロードがある場合とない場合で推論を比較します。関連する lib 定義と、issue #50387 にあるオーバーロードの順序に関する背景を確認します。このオーバーロードが存在する場合でも、例についてネストされた関数の推論が引き続き string を推論すれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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