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

未关闭
#64,974 1 条评论 3 个 reaction 已指派 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
派生
15.4k
平均合并
3 天 1 小时
30 天内合并 PR
136

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。