7.1: destructured parameter with a `= {}` default loses its contextual type (TS7031)

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

メンテナーはふだん 1 日以内に返信

@a-tarasyuk がすでに取り組んでいます。

2026年10月27日 から。

  • #64978 @a-tarasyuk による — オープン

評価

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

調査の方向性

Start by reading the compiler’s contextual parameter typing and destructuring code, then use the issue’s minimal reproduction to compare behavior across the affected 7.1 and working 7.0.2 versions. Add a regression test covering an optional contextually typed parameter with a destructuring pattern and an = {} default; done means both binding elements retain their contextual types without TS7031, while the two control cases still pass.

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

説明

Bug
🔎 Search Terms

TS7031, binding element implicitly has an any type, destructuring parameter default, contextual type, 7.1 regression

🕗 Version & Regression Information
  • This changed between versions 7.0.2 and 7.1.0-beta
  • Still present in 7.1.0-dev.20261026.1
  • 6.0.3 and 7.0.2 report no errors
⏯ Playground Link

No response

💻 Code
// tsconfig: { "compilerOptions": { "strict": true, "noEmit": true, "skipLibCheck": true, "types": [] } }
type Fn = (input?: { channel?: string; minutes?: number }) => number;
export const f: Fn = ({ channel, minutes } = {}) => (channel ? 1 : 0) + (minutes ?? 0);

The error needs all three of: a destructuring pattern, an = {} initializer, and a contextually typed optional parameter. Each of these alone still checks cleanly on 7.1:

type F2 = (input: { channel?: string; minutes?: number }) => number;
export const f2: F2 = ({ channel, minutes }) => (channel ? 1 : 0) + (minutes ?? 0); // no initializer: OK

type G = (input?: { channel?: string }) => number;
export const g: G = (input = {}) => (input.channel ? 1 : 0); // identifier, not a pattern: OK
🙁 Actual behavior
a.ts(2,25): error TS7031: Binding element 'channel' implicitly has an 'any' type.
a.ts(2,34): error TS7031: Binding element 'minutes' implicitly has an 'any' type.
🙂 Expected behavior

No errors: channel and minutes take their types (string | undefined, number | undefined) from the contextual parameter type, as on 6.0.3 and 7.0.2.

Additional information about the issue

Found while trialling 7.1.0-beta on a large monorepo; it fires in test helpers and callbacks typed through an alias (({ a, b } = {}) => … assigned to a function type with an optional object parameter).

主要言語
Go
スター
112k
フォーク
16k
平均マージ
3日 6時間
マージ済み PR(30日)
136

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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