microsoft / microsoft/TypeScript
[7.0 regression] Type parameter default is ignored when the call sits inside a callback passed to a generic function
まだ誰も着手していません。
- #63988 @copilot-swe-agent による — マージされずにクローズ
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
🔎 Search Terms
type parameter default
type parameter inference
default type argument
generic default
not assignable to parameter of type 'undefined'
tsgo generic
ts7 generic
🕗 Version & Regression Information
- This changed between versions 6.0.3 and 7.0.0
⏯ Playground Link
💻 Code
// TS 7.0.2 regression: spurious "not assignable to parameter of type 'undefined'"
//
// A generic function whose type parameter `T` has a *default* and is used both
// - in a parameter type (behind a conditional type), and
// - in its *inferred* return type
// loses that default when the call happens inside a callback that is itself the
// argument of another generic function (e.g. React's useCallback).
//
// TS 6.0.3: no error. TS 7.0.2 / 7.1.0-dev: error on the `data` argument.
declare function fetcher<D> (cfg: unknown): Promise<D>
type Routes<R> = R extends 'x' ? { IN: { v: number }, OUT: string } : never
// NOTE: no return type annotation -- the return type is inferred and mentions T.
function executeFetch<R extends string, T = Routes<R>> (
route: R,
body?: T extends Record<'IN', any> ? T['IN'] : never
) {
return fetcher<T extends Record<'OUT', any> ? T['OUT'] : T>({ route, body })
}
declare function useCallback<T extends Function> (cb: T, deps: unknown[]): T // eslint-disable-line
export const cb = useCallback((data: { v: number }) => {
return executeFetch('x', data)
// ^^^^
// TS 7: error TS2345: Argument of type '{ v: number; }' is not assignable
// to parameter of type 'undefined'.
// (i.e. `T` was not defaulted to Routes<'x'>, so the conditional took the
// false branch -> `never`, and `never | undefined` -> `undefined`)
}, [])
// The *result* type is still correct in both versions (Promise<string>), which
// shows the error comes from a discarded inference pass:
export const check: Promise<string> = cb({ v: 1 })
🙁 Actual behavior
Error: Argument of type '{ v: number; }' is not assignable to parameter of type 'undefined'.
🙂 Expected behavior
The proper type should be inferred on the function generic
Additional information about the issue
This issue was discovered in a large codebase and Claude Opus 5 was used to identify the issue and construct the reproduction.
It's worth noting that the TypeScript Playground does not support TS7 so this issue needs to be debugged locally (simply run tsc on the code and compare the errors (or lackthereof) between versions 6 and 7)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue で推奨されているとおり、tsc を使ってローカルの TypeScript 6 および 7 のビルドで提供された再現手順を実行し、診断結果を比較します。useCallback 内の executeFetch に対するジェネリック型パラメーターのデフォルト値と推論の挙動を追跡します。コールバックがエラーなしで { v: number } を受け入れ、同時に cb が Promise の結果型を保持すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100