Skip to content

Commit 0c4ceee

Browse files
author
Andy
authored
Don't consider 'typeof a' as using 'a' (microsoft#28528)
* Don't consider 'typeof a' as using 'a' * Also handle markPropertyAsReferenced * Use isInTypeQuery
1 parent b059135 commit 0c4ceee

7 files changed

+75
-2
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ namespace ts {
14781478
// We just climbed up parents looking for the name, meaning that we started in a descendant node of `lastLocation`.
14791479
// If `result === lastSelfReferenceLocation.symbol`, that means that we are somewhere inside `lastSelfReferenceLocation` looking up a name, and resolving to `lastLocation` itself.
14801480
// That means that this is a self-reference of `lastLocation`, and shouldn't count this when considering whether `lastLocation` is used.
1481-
if (isUse && result && (!lastSelfReferenceLocation || result !== lastSelfReferenceLocation.symbol)) {
1481+
if (isUse && result && (!lastSelfReferenceLocation || result !== lastSelfReferenceLocation.symbol) && !isInTypeQuery(originalLocation!)) {
14821482
result.isReferenced! |= meaning;
14831483
}
14841484

@@ -19022,7 +19022,7 @@ namespace ts {
1902219022
}
1902319023

1902419024
function markPropertyAsReferenced(prop: Symbol, nodeForCheckWriteOnly: Node | undefined, isThisAccess: boolean) {
19025-
if (!prop || !(prop.flags & SymbolFlags.ClassMember) || !prop.valueDeclaration || !hasModifier(prop.valueDeclaration, ModifierFlags.Private)) {
19025+
if (nodeForCheckWriteOnly && isInTypeQuery(nodeForCheckWriteOnly) || !prop || !(prop.flags & SymbolFlags.ClassMember) || !prop.valueDeclaration || !hasModifier(prop.valueDeclaration, ModifierFlags.Private)) {
1902619026
return;
1902719027
}
1902819028
if (nodeForCheckWriteOnly && isWriteOnlyAccess(nodeForCheckWriteOnly) && !(prop.flags & SymbolFlags.SetAccessor && !(prop.flags & SymbolFlags.GetAccessor))) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/unusedParameterUsedInTypeOf.ts(1,14): error TS6133: 'a' is declared but its value is never read.
2+
3+
4+
==== tests/cases/compiler/unusedParameterUsedInTypeOf.ts (1 errors) ====
5+
function f1 (a: number, b: typeof a) {
6+
~
7+
!!! error TS6133: 'a' is declared but its value is never read.
8+
return b;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/unusedPropertyUsedInTypeOf.ts(2,29): error TS6133: 'x' is declared but its value is never read.
2+
3+
4+
==== tests/cases/compiler/unusedPropertyUsedInTypeOf.ts (1 errors) ====
5+
class C {
6+
private static readonly x: number;
7+
~
8+
!!! error TS6133: 'x' is declared but its value is never read.
9+
m(p: typeof C.x) { return p; }
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [unusedPropertyUsedInTypeOf.ts]
2+
class C {
3+
private static readonly x: number;
4+
m(p: typeof C.x) { return p; }
5+
}
6+
7+
8+
//// [unusedPropertyUsedInTypeOf.js]
9+
var C = /** @class */ (function () {
10+
function C() {
11+
}
12+
C.prototype.m = function (p) { return p; };
13+
return C;
14+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/unusedPropertyUsedInTypeOf.ts ===
2+
class C {
3+
>C : Symbol(C, Decl(unusedPropertyUsedInTypeOf.ts, 0, 0))
4+
5+
private static readonly x: number;
6+
>x : Symbol(C.x, Decl(unusedPropertyUsedInTypeOf.ts, 0, 9))
7+
8+
m(p: typeof C.x) { return p; }
9+
>m : Symbol(C.m, Decl(unusedPropertyUsedInTypeOf.ts, 1, 38))
10+
>p : Symbol(p, Decl(unusedPropertyUsedInTypeOf.ts, 2, 6))
11+
>C.x : Symbol(C.x, Decl(unusedPropertyUsedInTypeOf.ts, 0, 9))
12+
>C : Symbol(C, Decl(unusedPropertyUsedInTypeOf.ts, 0, 0))
13+
>x : Symbol(C.x, Decl(unusedPropertyUsedInTypeOf.ts, 0, 9))
14+
>p : Symbol(p, Decl(unusedPropertyUsedInTypeOf.ts, 2, 6))
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/unusedPropertyUsedInTypeOf.ts ===
2+
class C {
3+
>C : C
4+
5+
private static readonly x: number;
6+
>x : number
7+
8+
m(p: typeof C.x) { return p; }
9+
>m : (p: number) => number
10+
>p : number
11+
>C.x : number
12+
>C : typeof C
13+
>x : number
14+
>p : number
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @noUnusedLocals:true
2+
// @noUnusedParameters:true
3+
4+
class C {
5+
private static readonly x: number;
6+
m(p: typeof C.x) { return p; }
7+
}

0 commit comments

Comments
 (0)