Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 9, 2025

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:

export class A<T = any> {
  public readonly field = <T>(): T => {
      return null as any
  }
}

export function needsRenameForShadowing<T>() {
  type A = T
  return function O<T>(t: A, t2: T) {
  }
}

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 becomes T_1):

export declare class A<T = any> {
    readonly field: <T_1>() => T_1;
}
export declare function needsRenameForShadowing<T>(): <T_1>(t: T, t2: T_1) => void;

Solution

The fix adds a TrackExistingEntityName method to the EmitResolver 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 the FlagsGenerateNamesForShadowedTypeParams flag that were already in the codebase, connecting them to the type parameter transformation logic.

Changes

  • Added TrackExistingEntityName method to EmitResolver interface
  • Implemented the method in the checker's emit resolver and nodebuilder
  • Updated transformTypeParameterDeclaration in the declaration transformer to use the potentially renamed name

Testing

All existing tests pass, including the new declarationEmitShadowing test cases imported from the TypeScript submodule that specifically verify this behavior.

Original prompt

From the TypeScript repo, port PR#61342

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.

@Copilot Copilot AI changed the title [WIP] Port PR#61342 to TypeScript Port TypeScript PR#61342: Rename type parameters when they are shadowed Oct 9, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh October 9, 2025 22:02
Copilot finished work on behalf of RyanCavanaugh October 9, 2025 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants