Skip to content

Commit 6ef2db5

Browse files
author
Andy
authored
inferFromUsage: Handle being at an unexpected location (microsoft#22569)
* inferFromUsage: Handle being at an unexpected location * add comment
1 parent 677d860 commit 6ef2db5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,21 @@ namespace ts.codefix {
6464
return undefined;
6565
}
6666

67+
const { parent } = token;
6768
switch (errorCode) {
6869
// Variable and Property declarations
6970
case Diagnostics.Member_0_implicitly_has_an_1_type.code:
7071
case Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined.code:
71-
annotateVariableDeclaration(changes, sourceFile, <PropertyDeclaration | PropertySignature | VariableDeclaration>token.parent, program, cancellationToken);
72-
return token.parent as Declaration;
72+
if (isVariableDeclaration(parent) || isPropertyDeclaration(parent) || isPropertySignature(parent)) { // handle bad location
73+
annotateVariableDeclaration(changes, sourceFile, parent, program, cancellationToken);
74+
return parent;
75+
}
76+
return undefined;
7377

7478
case Diagnostics.Variable_0_implicitly_has_an_1_type.code: {
7579
const symbol = program.getTypeChecker().getSymbolAtLocation(token);
76-
if (symbol && symbol.valueDeclaration) {
77-
annotateVariableDeclaration(changes, sourceFile, <VariableDeclaration>symbol.valueDeclaration, program, cancellationToken);
80+
if (symbol && symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration)) {
81+
annotateVariableDeclaration(changes, sourceFile, symbol.valueDeclaration, program, cancellationToken);
7882
return symbol.valueDeclaration;
7983
}
8084
}
@@ -95,7 +99,7 @@ namespace ts.codefix {
9599
// falls through
96100
case Diagnostics.Rest_parameter_0_implicitly_has_an_any_type.code:
97101
if (!seenFunctions || addToSeen(seenFunctions, getNodeId(containingFunction))) {
98-
const param = cast(token.parent, isParameter);
102+
const param = cast(parent, isParameter);
99103
annotateParameters(changes, param, containingFunction, sourceFile, program, cancellationToken);
100104
return param;
101105
}

0 commit comments

Comments
 (0)