Skip to content

Fix declaration emit leaking an alias's unused type parameter (Labeled<Label>) - #65594

Open
Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
mainfrom
copilot/fix-declaration-emit-issue
Open

Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
mainfrom
copilot/fix-declaration-emit-issue

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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.ts show the alias's own parameter instead of the type argument. The .d.ts then fails to compile.

// types.ts
export type Labeled<Label> = { id: number } & { name: string };
// main.ts
import type { Labeled } from "./types";
declare const item: Labeled<"user">;
export const copy = item;
// before: export declare const copy: Labeled<Label>;   // TS2552 in consumers
// after:  export declare const copy: Labeled<"user">;

Root cause

instantiateTypeWorker has 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 is Labeled<Label>. Same-file references and object, conditional, and indexed-access aliases don't go through this path.

Changes (instantiateTypeWorker)

  • Compare the full alias. The instantiated alias is now computed first, and t is reused only if the members, the alias symbol, and the alias type arguments are all unchanged.
  • Don't re-reduce intersections when only the alias changes. These cases are rebuilt with 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 with IntersectionFlagsNone collapsed them to string/number.
    • The members of an existing intersection are already reduced, so skipping reduction only affects types originally built without it. getIntersectionKey ignores this flag, so no duplicate type identities are created.
    • The check uses t.flags, so unions with an intersection origin are 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 the IntersectionFlagsNone rebuild and lose & {}. These are the results of running this PR's test with #65593's exact change applied to main:

Source type main #65593 This PR
Labeled<"user"> Labeled<Label> ❌ Labeled<"user"> Labeled<"user">
Branded<"user"> (number & {}) Branded<Label> ❌ number Branded<"user">
"alice" | "bob" | Loose<"user"> (string & {}) … | Loose<Label> ❌ string "alice" | "bob" | Loose<"user">
Relabeled<"user"> (= Branded<Label>) number number Relabeled<"user">
same-file LocalLoose<"user"> union string string "alice" | "bob" | LocalLoose<"user">

Under #65593 the declared type of names becomes string (see the .types baseline), so the literal completions are lost. The same-file collapse in the last row already happens on main.

Commits

  1. A new test with pre-fix baselines, which show the Label leak and the resulting TS2552 errors in the .d.ts.
  2. The full-alias comparison. At this commit the output matches Keep requested alias type arguments when instantiation leaves union/intersection members unchanged #65593, including the collapse.
  3. Skipping supertype reduction when only the alias changes.
  4. The accepted baselines.

…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>
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 20, 2026
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) changed the title [WIP] Fix declaration emit writes incorrect type parameter Fix declaration emit leaking an alias's unused type parameter (Labeled<Label>) Nov 20, 2026
@RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) marked this pull request as ready for review November 20, 2026 04:32

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.

🔵 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 & {}`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Declaration emit writes an alias's own type parameter (Labeled<Label>) instead of the type argument when the parameter is unused

4 participants