Skip to content

Keep requested alias type arguments when instantiation leaves union/intersection members unchanged - #65593

Closed
Kaique da Silva (ktfth) wants to merge 2 commits into
microsoft:mainfrom
ktfth:fix-65570-alias-type-arguments
Closed

Kaique da Silva (ktfth) wants to merge 2 commits into
microsoft:mainfrom
ktfth:fix-65570-alias-type-arguments

Conversation

@ktfth

Copy link
Copy Markdown

Fixes #65570

Problem. For an alias whose type parameter is unused, e.g. type Labeled<Label> = { id: number } & { name: string }, declaration emit printed Labeled<Label> instead of Labeled<"user">. Consumers of the .d.ts then fail with TS2552: Cannot find name 'Label'. It reproduces cross-file for intersection and union bodies and for number & {}.

Cause. instantiateTypeWithAlias intentionally lets these types through so they can be copied with new alias type arguments. But in instantiateTypeWorker, the union/intersection branch returned the original type when the members were unchanged and the requested alias had the same symbol. The original type is the declared type, whose alias type arguments are [Label].

Change. The shortcut also requires the alias type arguments to be equal (slices.Equal). Otherwise the type is re-created with the requested alias. For number & {} the intersection now reduces to number, as it does same-file.

Tests. New declarationEmitUnusedAliasTypeParameter.ts covers intersection, union, brand, inference through a generic call, and a used second parameter as a control. Without the change, its .d.ts has four TS2552 errors. On current main, go test ./internal/checker/ ./internal/testrunner/ passes with no other baseline changes, and gofmt and go vet ./internal/checker/ are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7

Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:41
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 19, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels 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 are reported for the alias type-argument preservation change.

No open issues 🎉

What changed in this PR

Fixes #65570 by preserving requested alias type arguments during union and intersection instantiation, preventing declaration emit from referencing out-of-scope type parameters.

Changes:

  • Require matching alias type arguments before reusing an unchanged union or intersection type.
  • Add cross-file regression coverage for unused parameters, generic inference, and a used-parameter control.
  • Record expected declaration output, types, and symbols, including reduction of number & {} to number.
File Description
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitUnusedAliasTypeParameter.ts Adds declaration-emit regression cases.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnusedAliasTypeParameter.types Records expected instantiated types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnusedAliasTypeParameter.symbols Records symbol bindings for the test.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnusedAliasTypeParameter.js Records JavaScript and corrected declaration output.
tsc/​internal/​checker/​checker.go Checks alias arguments before reusing an existing type.

Review effort: Balanced

@ktfth

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@jakebailey

Copy link
Copy Markdown
Member

If you're willing, this would be much easier to review if it were two commits, one that adds the test and baselines showing bad behavior, then one commit with the fix and baseline update showing it works.

…eter

The baselines record the current behavior: the declaration file prints `Labeled<Label>` instead of
`Labeled<"user">`, and its four TS2552 errors.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
…ntersection members unchanged

instantiateTypeWorker returned the declared union/intersection type whenever its members were
unchanged and the requested alias had the same symbol. For an alias whose type parameter is
unused (`type Labeled<Label> = { id: number } & { name: string }`), instantiating
`Labeled<"user">` then returned the declared type carrying alias arguments [Label], and
declaration emit printed `Labeled<Label>`, an out-of-scope name.

Also compare the alias type arguments before taking the shortcut.

Fixes microsoft#65570

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
@ktfth
Kaique da Silva (ktfth) force-pushed the fix-65570-alias-type-arguments branch from 70f88b6 to 8aeb67a Compare November 20, 2026 02:25
@RyanCavanaugh

Copy link
Copy Markdown
Member

Closing in favor of #65594 which has clearer baselines and maybe a second-bug-in-a-trenchcoat fix as well

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

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Done

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