Skip to content

Commit e8729f8

Browse files
Only keep binding element aliases if they are used in the signature.
1 parent 0055c91 commit e8729f8

12 files changed

+1262
-84
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6121,12 +6121,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
61216121

61226122
function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMakeVisible: boolean): SymbolVisibilityResult | undefined {
61236123
let aliasesToMakeVisible: LateVisibilityPaintedStatement[] | undefined;
6124+
let bindingElementToMakeVisible: BindingElement | undefined;
61246125
if (!every(filter(symbol.declarations, d => d.kind !== SyntaxKind.Identifier), getIsDeclarationVisible)) {
61256126
return undefined;
61266127
}
6127-
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible };
6128+
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible, bindingElementToMakeVisible };
61286129

61296130
function getIsDeclarationVisible(declaration: Declaration) {
6131+
if (isBindingElement(declaration)) {
6132+
bindingElementToMakeVisible = declaration;
6133+
}
61306134
if (!isDeclarationVisible(declaration)) {
61316135
// Mark the unexported alias as visible if its parent is visible
61326136
// because these kind of aliases can be used to name types in declaration file

src/compiler/transformers/declarations.ts

Lines changed: 122 additions & 19 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5517,6 +5517,7 @@ export type LateVisibilityPaintedStatement =
55175517
/** @internal */
55185518
export interface SymbolVisibilityResult {
55195519
accessibility: SymbolAccessibility;
5520+
bindingElementToMakeVisible?: BindingElement;
55205521
aliasesToMakeVisible?: LateVisibilityPaintedStatement[]; // aliases that need to have this symbol visible
55215522
errorSymbolName?: string; // Optional symbol name that results in error
55225523
errorNode?: Node; // optional node that results in error
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
declarationEmitBindingPatternsUnused.ts(81,35): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
2+
declarationEmitBindingPatternsUnused.ts(85,41): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
3+
declarationEmitBindingPatternsUnused.ts(91,11): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
4+
declarationEmitBindingPatternsUnused.ts(92,15): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
5+
declarationEmitBindingPatternsUnused.ts(93,16): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
6+
declarationEmitBindingPatternsUnused.ts(94,12): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
7+
8+
9+
==== declarationEmitBindingPatternsUnused.ts (6 errors) ====
10+
type Named = { name: string }
11+
function notReferenced({ name: alias }: Named) {
12+
13+
}
14+
function notReferencedNestedAlias({ p: { name: alias } }: { p: Named }) {
15+
}
16+
function notReferencedArrayAlias([a, b, { name: alias }]: Named[]) {
17+
}
18+
19+
20+
21+
function referencedInCode({ name: alias }: Named) {
22+
return alias;
23+
}
24+
25+
function referencedInSignarture({ name: alias }: Named): typeof alias {
26+
return alias;
27+
}
28+
29+
function referencedInInferredType({ name: alias }: Named) {
30+
type Named2 = { name: typeof alias }
31+
return null! as Named2
32+
}
33+
34+
function referencedInNestedFunction({ name: alias }: Named) {
35+
return function(p: typeof alias) {
36+
37+
}
38+
}
39+
40+
function referencedNestedAlias({ p: { name: alias } }: { p: Named }): typeof alias {
41+
return alias;
42+
}
43+
44+
function referencedArrayAlias([a, b, { name: alias }]: Named[]): typeof alias {
45+
return alias;
46+
}
47+
48+
49+
class NotReferencedClass {
50+
constructor({ name: alias }: Named) {
51+
}
52+
set x({ name: alias }: Named) {
53+
console.log(alias);
54+
}
55+
m({ name: alias }: Named) {
56+
console.log(alias);
57+
}
58+
}
59+
60+
class ReferencedInCodeClas {
61+
constructor({ name: alias }: Named) {
62+
console.log(alias);
63+
}
64+
set x({ name: alias }: Named) {
65+
console.log(alias);
66+
}
67+
m({ name: alias }: Named) {
68+
console.log(alias);
69+
}
70+
}
71+
72+
class ReferencedInSignartureClass {
73+
constructor({ name: alias }: Named, p: typeof alias) {
74+
console.log(alias);
75+
}
76+
set x({ name: alias }: Named & { o: typeof alias }) {
77+
78+
}
79+
mReturnType({ name: alias }: Named): typeof alias {
80+
return null!
81+
}
82+
mRerturnTypeNested({ name: alias }: Named): NonNullable<typeof alias> {
83+
return null!
84+
}
85+
mParameter({ name: alias }: Named, p: typeof alias) {
86+
return null!
87+
}
88+
}
89+
90+
let notReferencedFnType: ({ name: alias }: Named) => void;
91+
~~~~~
92+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
93+
let referencedInSignartureReturnTypeFnType: ({ name: alias }: Named) => typeof alias;
94+
let referencedInSignartureParamTypeFnType: ({ name: alias }: Named, p: typeof alias) => void;
95+
96+
let notReferencedCtorType: new ({ name: alias }: Named) => void;
97+
~~~~~
98+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
99+
let referencedInSignartureReturnTypeCtorType: new ({ name: alias }: Named) => typeof alias;
100+
let referencedInSignartureParamTypeCtorType: new ({ name: alias }: Named, p: typeof alias) => void;
101+
102+
103+
interface NotReferencedInterface {
104+
({ name: alias }: Named): void
105+
~~~~~
106+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
107+
new ({ name: alias }: Named): void
108+
~~~~~
109+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
110+
set x({ name: alias }: Named);
111+
~~~~~
112+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
113+
m({ name: alias }: Named);
114+
~~~~~
115+
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
116+
}
117+
118+
interface ReferencedInSignartureInterface {
119+
({ name: alias }: Named, p: typeof alias): void
120+
({ name: alias }: Named): typeof alias
121+
122+
new ({ name: alias }: Named, p: typeof alias): void
123+
new ({ name: alias }: Named): typeof alias
124+
set x({ name: alias }: Named & { o: typeof alias })
125+
mReturnType({ name: alias }: Named): typeof alias;
126+
mRerturnTypeNested({ name: alias }: Named): NonNullable<typeof alias>;
127+
mParameter({ name: alias }: Named, p: typeof alias);
128+
}

0 commit comments

Comments
 (0)