Repository navigation
Fix declaration emit leaking an alias's unused type parameter (Labeled<Label>) - #65594
Open
Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
Open
Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
Conversation
…e unused
Cross-file references to such aliases leak the alias's own type parameter
(Labeled<Label>) into types and declaration emit, and same-file references
to string & {} / number & {} aliases lose the {} constituent.
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
…r intersection When an alias doesn't reference its type parameters, instantiating it leaves the members unchanged. The shortcut in instantiateTypeWorker then returned the declared type whenever the alias symbols matched, ignoring the alias type arguments. References through an import pass in an alias with the same symbol, so the declared type's alias (e.g. Labeled<Label>) leaked out of its scope. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Intersections like string & {} or number & {} are created without supertype
reduction so that, e.g., "a" | "b" | (string & {}) keeps its literal members.
That isn't recorded on the type, so copying the intersection with new alias type
arguments reduced it to plain string/number. The members of an existing
intersection are already reduced, so skip supertype reduction when only the
alias changes.
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Labeled<Label>)
Ryan Cavanaugh (RyanCavanaugh)
marked this pull request as ready for review
November 20, 2026 04:32
Ryan Cavanaugh (RyanCavanaugh)
requested review from
Wesley Wigham (weswigham)
and
a balanced review from Copilot
November 20, 2026 04:32
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The core intersection-normalization changes affect same-file behavior beyond the declaration leak, and the compiler/fourslash suite could not be run, leaving broad baseline stability for human confirmation.
No open issues 🎉
What changed in this PR
Fixes #65570 by preserving supplied type arguments when instantiating union and intersection aliases, preventing declaration emit from leaking out-of-scope type parameters.
Changes:
- Compare both alias symbols and type arguments before reusing an existing type.
- Preserve intersections such as
string & {}when only the alias changes. - Add regression coverage and baselines for imported aliases, local aliases, and generic inference.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/declarationEmitUnusedAliasTypeParameter.ts |
Adds declaration-emit and alias-preservation regression cases. |
tsc/testdata/baselines/reference/compiler/declarationEmitUnusedAliasTypeParameter.types |
Records correct type arguments and preserved literal unions. |
tsc/testdata/baselines/reference/compiler/declarationEmitUnusedAliasTypeParameter.symbols |
Records symbol bindings for the regression cases. |
tsc/testdata/baselines/reference/compiler/declarationEmitUnusedAliasTypeParameter.js |
Records JavaScript and corrected declaration output. |
tsc/internal/checker/checker.go |
Checks full alias identity and avoids unnecessary intersection reduction. |
Review effort: Balanced
| return t | ||
| } | ||
| // Only the alias changed, as in an alias that doesn't reference its type parameters. The constituents | ||
| // of an intersection are already reduced, so don't reduce again; that would drop the {} in `string & {}`. |
There was a problem hiding this comment.
Given this, I assume when we take Wesley's changes to drop this construct, none of this will matter?
This branch has not been deployed
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.
Some imported aliases don't use their type parameter. When a union or intersection alias like that is used in another file, its type and the emitted
.d.tsshow the alias's own parameter instead of the type argument. The.d.tsthen fails to compile.Root cause
instantiateTypeWorkerhas a union/intersection shortcut. If the members are unchanged and the alias symbol matches, it returns the original type without checking the alias type arguments.A reference through an import passes an explicit alias (
{Labeled, ["user"]}) with that same symbol. The shortcut therefore returns the declared type, whose alias isLabeled<Label>. Same-file references and object, conditional, and indexed-access aliases don't go through this path.Changes (
instantiateTypeWorker)tis reused only if the members, the alias symbol, and the alias type arguments are all unchanged.IntersectionFlagsNoSupertypeReduction.string & {},number & {}, and`a${string}` & {}are created without supertype reduction, which is how"a" | "b" | (string & {})keeps its literals. That flag isn't stored on the type, so rebuilding withIntersectionFlagsNonecollapsed them tostring/number.getIntersectionKeyignores this flag, so no duplicate type identities are created.t.flags, so unions with an intersectionoriginare still rebuilt with normal reduction.Why not the one-line fix (#65593)
#65593 adds a
slices.Equal(alias.TypeArguments(), …)check to the existing shortcut. That stops the leak, but these types then fall through to theIntersectionFlagsNonerebuild and lose& {}. These are the results of running this PR's test with #65593's exact change applied tomain:mainLabeled<"user">Labeled<Label>❌Labeled<"user">Labeled<"user">Branded<"user">(number & {})Branded<Label>❌numberBranded<"user">"alice" | "bob" | Loose<"user">(string & {})… | Loose<Label>❌string"alice" | "bob" | Loose<"user">Relabeled<"user">(=Branded<Label>)numbernumberRelabeled<"user">LocalLoose<"user">unionstringstring"alice" | "bob" | LocalLoose<"user">Under #65593 the declared type of
namesbecomesstring(see the.typesbaseline), so the literal completions are lost. The same-file collapse in the last row already happens onmain.Commits
Labelleak and the resulting TS2552 errors in the.d.ts.Labeled<Label>) instead of the type argument when the parameter is unused #65570