Skip to content

Fix contextual typing loss for computed string keys under circular mapped-type key constraints - #65575

Closed
5 (tydavid5) wants to merge 1 commit into
microsoft:mainfrom
tydavid5:fix/ts7006-circular-mapped-key-65530
Closed

5 (tydavid5) wants to merge 1 commit into
microsoft:mainfrom
tydavid5:fix/ts7006-circular-mapped-key-65530

Conversation

@tydavid5

Copy link
Copy Markdown

Fixes #65530

This is a 6.0.3 to 7.x regression. When a mapped type's key constraint resolves to the error type because its base constraint is circular, resolveMappedTypeMembers treated it as an any key and synthesized a spurious [x: string]: undefined index signature. That signature merged with the contextual type of a computed string key, so the callback parameter lost its contextual type and was reported as implicitly any (TS7006). The repro in the issue shows it: indexed.derive with a computed key errors on 7.x but not on 6.0.3, while a literal key and a constraint without self-reference are fine.

The fix skips member creation when the key constraint is the error type with a circular base constraint, so such a mapped type contributes no members. Genuine string and any keys and unresolved-name key types keep their existing behavior.

A regression test covers the reported repro plus controls for genuine [x: string]: undefined index signatures, legitimate any-keyed mapped types, and an unresolved key name.

I verified the repro before the fix (TS7006 at main.ts 9,33) and after (no errors), and ran the new compiler test plus the contextualType, mappedType and indexSignature test groups, all passing.

…pped-type key constraints

When a mapped type's key constraint resolves to the error type because its base constraint is circular, resolveMappedTypeMembers treated it as an any key and synthesized a spurious [x: string]: undefined index signature. That signature merged with the contextual type of computed string keys, so callback parameters lost their contextual type and were reported as implicitly any (TS7006). This is a 6.0.3 to 7.x regression.

The fix skips member creation when the key constraint is the error type with a circular base constraint, so such a mapped type contributes no members. Genuine string and any keys and unresolved-name key types keep their existing behavior, and a regression test covers the reported repro plus those controls.

Fixes microsoft#65530
Copilot AI balanced review requested due to automatic review settings November 19, 2026 07:51
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 19, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the fix for contextual typing with circular mapped-type key constraints.

No open issues 🎉

What changed in this PR

Fixes #65530, a TypeScript 7.x regression where circular mapped-type key constraints caused callback parameters under computed string keys to lose contextual typing.

Changes:

  • Skip mapped-type member creation when an error-type key constraint has a circular base constraint.
  • Add regression coverage and controls preserving existing behavior for genuine index signatures, any keys, and unresolved key names.
  • Record expected diagnostics, inferred types, and symbols.
File Description
tsc/​testdata/​tests/​cases/​compiler/​contextualTypeWithCircularMappedKeyConstraint.ts Adds the regression test and control cases.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypeWithCircularMappedKeyConstraint.types Records restored contextual typing and control-case types.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypeWithCircularMappedKeyConstraint.symbols Records expected symbol resolution.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypeWithCircularMappedKeyConstraint.errors.txt Records expected errors for the control cases.
tsc/​internal/​checker/​checker.go Prevents circular error constraints from creating spurious index signatures.

Review effort: Balanced

@tydavid5

Copy link
Copy Markdown
Author

Note: this takes the same approach as #65546, which was closed without review. I checked the approach, reproduced the bug on main first, and verified the fix with the repro and the new regression test.

@tydavid5

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

TS7 regression: callback parameter loses contextual type (TS7006) for a computed string key when the type parameter's constraint references itself

3 participants