Repository navigation
Fix contextual typing loss for computed string keys under circular mapped-type key constraints - #65575
Closed
5 (tydavid5) wants to merge 1 commit into
Closed
Fix contextual typing loss for computed string keys under circular mapped-type key constraints#655755 (tydavid5) wants to merge 1 commit into
5 (tydavid5) wants to merge 1 commit into
Conversation
…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
Contributor
There was a problem hiding this comment.
🟢 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,
anykeys, 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
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. |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.