Port TypeScript PR#61342: Rename type parameters when they are shadowed #1860
+68
−2
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.
This PR ports microsoft/TypeScript#61342 which fixes an issue where shadowed type parameters were not being properly renamed in declaration files, leading to invalid or ambiguous type declarations.
Problem
When type parameters in inner scopes shadow type parameters from outer scopes, the declaration emitter needs to rename them to avoid ambiguity. For example:
Without this fix, the emitted declaration file would have ambiguous references to
T
. With this fix, the inner type parameters are renamed (e.g.,T
becomesT_1
):Solution
The fix adds a
TrackExistingEntityName
method to theEmitResolver
interface that checks if a type parameter name is shadowed and returns a renamed version if necessary. The declaration transformer now calls this method when processing type parameter declarations instead of directly using the original name.This leverages the existing
typeParameterToName
infrastructure and theFlagsGenerateNamesForShadowedTypeParams
flag that were already in the codebase, connecting them to the type parameter transformation logic.Changes
TrackExistingEntityName
method toEmitResolver
interfacetransformTypeParameterDeclaration
in the declaration transformer to use the potentially renamed nameTesting
All existing tests pass, including the new
declarationEmitShadowing
test cases imported from the TypeScript submodule that specifically verify this behavior.Original prompt
Note
Custom agent used: Strada to Corsa Port Expert
A Go and TypeScript expert who can easily figure out how to port PRs from one language to another
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.