microsoft / microsoft/TypeScript

Unexpected type infer for function first argument with default value since 5.1

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

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

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

説明

🔎 Search Terms

"type infer first argument"

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions v5.0.4 and v5.1.6
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240308#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXy1URBgEYAeAFQD4AKRVALngDEpMcYBPK6gSmaUAUKEiwEKdNjwEiJAEy96TVuwycuimgPjChIAB4AHThngYuRhGw7de8ALzxasAOalBAGnhv5zKKhcfI7UPoH6xqbmltZqGlqhTrQAdKluAM7MANqU3gFcALrBDqH5QuVgeOlmiKSOzgC26a7M1TCErvUA5AAWIBAQOF3eGCDV-oHFoQDe8AC+QpWo1T51ToTEZPSkfEIA9HvwR-AAegD8i1VmUPL1GwrbuwfHpxeXyzW3SU0t8G0d3T6AyGU3gswWSxWACM7nItoh5E9Dsdzu9oWtZJt5PREftkUdUWizGBYZtSLRaD9Whh2qhOk5ev1Bl1QeCkS9UZDiV9MQ9Kc1qbT6fBGcCWSEwfN2Si3kA

💻 Code
export type Factory<T> = (arg1: T, arg2: any) => any
export type Factory2<T> = (...args: [T, any]) => any

declare function infer1<T>(fn: Factory<T>): T
declare function infer2<T>(fn: Factory2<T>): T

const f1 = (msg: string = 'hello', test: any) => { }
const a1 = infer1(f1)
//    ^? const a1: string | undefined
const a2 = infer2(f1)
//    ^? const a2: string | undefined

const f2 = (msg: string = 'hello') => { }
const b = infer1(f2)
//    ^? const b: string | undefined
const b1 = infer2(f2)
//    ^? const b1: string | undefined

const c = infer1((msg: string = 'hello') => { })
//    ^? const c: string
const c2 = infer2((msg: string = 'hello') => { })
//    ^? const c2: string | undefined
🙁 Actual behavior

In my code you'll notice that the type of c is not the same as b, but the argument we passing into function infer1 is actually the same.

I've tested this between v5.0.4 and v5.1.6, but the type of c is different:

// v5.0.4
const c: string | undefined

// v5.1.6
const c: string

After checking v5.1 changelog, I'm still not sure what changes have been made between 5.0 to 5.1.

So I decided to submit this issue as a "Bug report".

🙂 Expected behavior

The type of c is supposed to be string | undefined.

Additional information about the issue

No response

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground から始め、提供されたジェネリック推論の例を縮小しながら、TypeScript 5.0.4 と 5.1.6 を比較します。デフォルト値を持つ最初のパラメーターに対する型推論の挙動を追跡し、報告された結果に一貫して undefined が含まれるべきかどうかを判断します。関連するケースをリグレッションさせることなく、最小限の repro が期待される型と一致すれば完了です。

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

評価

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

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

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