Skip to content

Detect self-referencing mapped type constraints - #64538

Open
Naji Ullah (Naji-Ullah) wants to merge 2 commits into
microsoft:mainfrom
Naji-Ullah:fix-62176
Open

Naji Ullah (Naji-Ullah) wants to merge 2 commits into
microsoft:mainfrom
Naji-Ullah:fix-62176

Conversation

@Naji-Ullah

Copy link
Copy Markdown

With this PR we report a circularity error when the type parameter of a mapped type is referenced in its own constraint. Previously such circularities were detected only when base constraint resolution happened to revisit the type parameter (as in { [P in P]: X }), but not when the reference occurred in a position whose base constraint doesn't depend on it, such as keyof P. In the example

type Test = keyof {
    [P in keyof P]: unknown
};

declare const test: Test;

export function getTest() {
    return test;
}

no error was reported, and P escaped from the mapped type into the generated declaration file:

export declare function getTest(): keyof P;

We now report

Type parameter 'P' has a circular constraint.

on keyof P, the same error we already report for { [P in P]: X }, and we use the error type as the constraint so P no longer escapes (the declaration above becomes getTest(): any). The check happens where the declared constraint of a mapped type parameter is resolved, so it also covers instantiations of generic mapped types. It applies only to mapped types, so ordinary type parameters such as T extends keyof T are unaffected. Any reference to the type parameter within its own constraint is considered circular, including one that doesn't affect the resulting keys, such as { [P in keyof { a: P }]: X }.

The new test circularMappedTypeConstraint2.ts covers the example above, variants such as keyof P | "x", Extract<keyof P, string>, keyof Cond<P>, and a generic mapped type and its instantiation, as well as cases that must remain error-free (T extends keyof T, a shadowed inner P, and an as clause referencing P). No existing baselines are affected.

AI disclosure: I used an AI coding assistant (Claude Code) while investigating and writing this fix. I've reviewed the change and the tests, I understand them, and I'll be handling review feedback myself.

Fixes #62176.

Copilot AI balanced review requested due to automatic review settings September 29, 2026 17:19
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 29, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Sep 29, 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.

Copilot review overview

🟢 Approval recommended

The implementation is narrowly scoped and its diagnostic, recovery, instantiation, shadowing, and declaration-emit behavior are covered by regression baselines.

Review effort: Balanced
Findings: None

What changed in this PR

Adds circular-constraint detection for self-referencing mapped type parameters and prevents them from leaking into declaration output.

Changes:

  • Detect mapped type parameters referenced within their own constraints.
  • Substitute the error type after reporting circularity.
  • Add regression tests and compiler baselines for errors, types, symbols, and declaration emit.
File Description
tsc/​internal/​checker/​checker.go Implements self-reference detection and error recovery.
tsc/​testdata/​tests/​cases/​compiler/​circularMappedTypeConstraint2.ts Adds positive and negative regression cases.
tsc/​testdata/​baselines/​reference/​compiler/​circularMappedTypeConstraint2.errors.txt Records expected diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​circularMappedTypeConstraint2.js Verifies JavaScript and declaration output.
tsc/​testdata/​baselines/​reference/​compiler/​circularMappedTypeConstraint2.types Records inferred types.
tsc/​testdata/​baselines/​reference/​compiler/​circularMappedTypeConstraint2.symbols Records symbol resolution.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@Naji-Ullah

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
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: Not started

Development

Successfully merging this pull request may close these issues.

No error on self-referencing type parameter of non-generic mapped type

2 participants