Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17f68c3

Browse files
committedMar 3, 2025·
Rename type parameters when they are shadowed.
1 parent 1539234 commit 17f68c3

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed
 

‎src/compiler/expressionToTypeNode.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1122,15 +1122,16 @@ export function createSyntacticTypeNodeBuilder(
11221122
);
11231123
}
11241124
function reuseTypeParameters(typeParameters: NodeArray<TypeParameterDeclaration> | undefined, context: SyntacticTypeNodeBuilderContext) {
1125-
return typeParameters?.map(tp =>
1126-
factory.updateTypeParameterDeclaration(
1125+
return typeParameters?.map(tp => {
1126+
const { node: tpName } = resolver.trackExistingEntityName(context, tp.name);
1127+
return factory.updateTypeParameterDeclaration(
11271128
tp,
11281129
tp.modifiers?.map(m => reuseNode(context, m)),
1129-
reuseNode(context, tp.name),
1130+
tpName,
11301131
serializeExistingTypeNodeWithFallback(tp.constraint, context),
11311132
serializeExistingTypeNodeWithFallback(tp.default, context),
1132-
)
1133-
);
1133+
);
1134+
});
11341135
}
11351136

11361137
function typeFromObjectLiteralMethod(method: MethodDeclaration, name: PropertyName, context: SyntacticTypeNodeBuilderContext, isConstContext: boolean) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////
2+
3+
//// [declarationEmitShadowing.ts]
4+
export class A<T = any> {
5+
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
6+
return null as any
7+
}
8+
}
9+
10+
export function needsRenameForShadowing<T>() {
11+
type A = T
12+
return function O<T>(t: A, t2: T) {
13+
}
14+
}
15+
16+
17+
//// [declarationEmitShadowing.js]
18+
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
20+
exports.A = void 0;
21+
exports.needsRenameForShadowing = needsRenameForShadowing;
22+
var A = /** @class */ (function () {
23+
function A() {
24+
this.ShadowedButDoesNotRequireRenaming = function () {
25+
return null;
26+
};
27+
}
28+
return A;
29+
}());
30+
exports.A = A;
31+
function needsRenameForShadowing() {
32+
return function O(t, t2) {
33+
};
34+
}
35+
36+
37+
//// [declarationEmitShadowing.d.ts]
38+
export declare class A<T = any> {
39+
readonly ShadowedButDoesNotRequireRenaming: <T_1>() => T_1;
40+
}
41+
export declare function needsRenameForShadowing<T>(): <T_1>(t: T, t2: T_1) => void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////
2+
3+
=== declarationEmitShadowing.ts ===
4+
export class A<T = any> {
5+
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 0, 0))
6+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 0, 15))
7+
8+
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
9+
>ShadowedButDoesNotRequireRenaming : Symbol(A.ShadowedButDoesNotRequireRenaming, Decl(declarationEmitShadowing.ts, 0, 25))
10+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 1, 55))
11+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 1, 55))
12+
13+
return null as any
14+
}
15+
}
16+
17+
export function needsRenameForShadowing<T>() {
18+
>needsRenameForShadowing : Symbol(needsRenameForShadowing, Decl(declarationEmitShadowing.ts, 4, 1))
19+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 6, 40))
20+
21+
type A = T
22+
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 6, 46))
23+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 6, 40))
24+
25+
return function O<T>(t: A, t2: T) {
26+
>O : Symbol(O, Decl(declarationEmitShadowing.ts, 8, 8))
27+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 8, 20))
28+
>t : Symbol(t, Decl(declarationEmitShadowing.ts, 8, 23))
29+
>A : Symbol(A, Decl(declarationEmitShadowing.ts, 6, 46))
30+
>t2 : Symbol(t2, Decl(declarationEmitShadowing.ts, 8, 28))
31+
>T : Symbol(T, Decl(declarationEmitShadowing.ts, 8, 20))
32+
}
33+
}
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [tests/cases/compiler/declarationEmitShadowing.ts] ////
2+
3+
=== declarationEmitShadowing.ts ===
4+
export class A<T = any> {
5+
>A : A<T>
6+
> : ^^^^
7+
8+
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
9+
>ShadowedButDoesNotRequireRenaming : <T_1>() => T_1
10+
> : ^^^^^^^^^^^
11+
><T>(): T => { return null as any } : <T_1>() => T_1
12+
> : ^^^^^^^^^^^
13+
14+
return null as any
15+
>null as any : any
16+
}
17+
}
18+
19+
export function needsRenameForShadowing<T>() {
20+
>needsRenameForShadowing : <T>() => <T_1>(t: T, t2: T_1) => void
21+
> : ^ ^^^^^^^^ ^^ ^^^ ^^^^^^^^^
22+
23+
type A = T
24+
>A : T
25+
> : ^
26+
27+
return function O<T>(t: A, t2: T) {
28+
>function O<T>(t: A, t2: T) { } : <T_1>(t: T, t2: T_1) => void
29+
> : ^^^^^^ ^^^^^ ^^ ^^^^^^^^^
30+
>O : <T>(t: T_1, t2: T) => void
31+
> : ^ ^^ ^^^^^^^ ^^ ^^^^^^^^^
32+
>t : T_1
33+
> : ^^^
34+
>t2 : T
35+
> : ^
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @declaration: true
2+
3+
export class A<T = any> {
4+
public readonly ShadowedButDoesNotRequireRenaming = <T>(): T => {
5+
return null as any
6+
}
7+
}
8+
9+
export function needsRenameForShadowing<T>() {
10+
type A = T
11+
return function O<T>(t: A, t2: T) {
12+
}
13+
}

0 commit comments

Comments
 (0)